How to add associative array Elements together grouped by their indexHow to check if PHP array is associative or sequential?Deleting an element from an array in PHPHow to add elements to an empty array in PHP?Get first key in a (possibly) associative array?How to sort an array of associative arrays by value of a given key in PHP?Get the first element of an arrayHow to Sort Multi-dimensional Array by Value?Remove empty array elementsConvert PHP object to associative arrayHow do I display this array correctly in html?

Read file lines into shell line separated by space

How can I reorder triggered abilities in Arena?

Filling a listlineplot with a texture

"Opusculum hoc, quamdiu vixero, doctioribus emendandum offero."?

Heyacrazy: No Diagonals

Nothing like a good ol' game of ModTen

Sum ergo cogito?

Breaker Mapping Questions

What is the right path to be followed on Chess.com for learning Chess?

Athens airport 1-hour connection at normal walking speed

How to make onclick function execute only once?

How to find out the average duration of the peer-review process for a given journal?

How to change image size without scaling in GIMP

What is the loud noise of a helicopter when the rotors are not yet moving?

Who was the most successful German spy against Great Britain in WWII, from the contemporary German perspective?

Round towards zero

Is first Ubuntu user root?

Papers on arXiv solving the same problem at the same time

How do proponents of Sola Scriptura address the ministry of those Apostles who authored no parts of Scripture?

Handling Disruptive Student on the Autism Spectrum

Was there ever a treaty between 2 entities with significantly different translations to the detriment of one party?

Algorithms vs LP or MIP

Understanding how the determinant of the multidimensional normal likelihood can overrule the prior probability

Do Bayesian credible intervals treat the estimated parameter as a random variable?



How to add associative array Elements together grouped by their index


How to check if PHP array is associative or sequential?Deleting an element from an array in PHPHow to add elements to an empty array in PHP?Get first key in a (possibly) associative array?How to sort an array of associative arrays by value of a given key in PHP?Get the first element of an arrayHow to Sort Multi-dimensional Array by Value?Remove empty array elementsConvert PHP object to associative arrayHow do I display this array correctly in html?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















My boss wants to show the most popular states that people request franchising data from. I'm working with zipcodes that users put in. I run it through a function to find the state, but the problem is I have a count for each zip code not a total for the state itself. I need to combine the totals for each zip code(into one total for the state).



I'm able to add all these up by putting a counter in the loop but it does not break for each state. So I guess the solution would be a way break the loop when the state changes? so currently the output is as such:



New York - 8
New York - 11
New York -236
New York - 7
California - 2
Alabama - 7



I need to add all of new york into one category.



<?php
include("databaseClass.php");
$databaseClass = new databaseClass();
$conn = $databaseClass->connect();
$countFranArr = array();
$running = 0;


$sqlFranCount = "select client_zip, count(*) as total from request_form_commits inner join franchise on franchise.franchise_id = request_form_commits.franchise_id
where time_of_request >= '2011-03-20 18:01:04' and time_of_request <= '2033-03-20 21:03:22' group by client_zip";
$resultFranCount = $conn->query($sqlFranCount);
if($resultFranCount->num_rows > 0)

while($rowFranCount = $resultFranCount->fetch_assoc())

//array_push_assoc($countCatArr, $rowCatCount['fdd_category'], $rowCatCount['total']);
//echo $rowCatCount['total']."<br>";
$countFranArr[$rowFranCount['client_zip']] = $rowFranCount['total'];




//arsort($countFranArr);

foreach($countFranArr as $zip=>$total)

//echo $databaseClass->getState($zip)." - ".$total."<br>";
$state = $databaseClass->getState($zip);
if($state == "")

echo "";

else
echo $state." - ".$total."<br>";
$running = $running + $total;



echo $running;


?>


Expected results will be



New York 267
California - 3
etc...



One total count for each state so I can order them by most popular










share|improve this question






























    0















    My boss wants to show the most popular states that people request franchising data from. I'm working with zipcodes that users put in. I run it through a function to find the state, but the problem is I have a count for each zip code not a total for the state itself. I need to combine the totals for each zip code(into one total for the state).



    I'm able to add all these up by putting a counter in the loop but it does not break for each state. So I guess the solution would be a way break the loop when the state changes? so currently the output is as such:



    New York - 8
    New York - 11
    New York -236
    New York - 7
    California - 2
    Alabama - 7



    I need to add all of new york into one category.



    <?php
    include("databaseClass.php");
    $databaseClass = new databaseClass();
    $conn = $databaseClass->connect();
    $countFranArr = array();
    $running = 0;


    $sqlFranCount = "select client_zip, count(*) as total from request_form_commits inner join franchise on franchise.franchise_id = request_form_commits.franchise_id
    where time_of_request >= '2011-03-20 18:01:04' and time_of_request <= '2033-03-20 21:03:22' group by client_zip";
    $resultFranCount = $conn->query($sqlFranCount);
    if($resultFranCount->num_rows > 0)

    while($rowFranCount = $resultFranCount->fetch_assoc())

    //array_push_assoc($countCatArr, $rowCatCount['fdd_category'], $rowCatCount['total']);
    //echo $rowCatCount['total']."<br>";
    $countFranArr[$rowFranCount['client_zip']] = $rowFranCount['total'];




    //arsort($countFranArr);

    foreach($countFranArr as $zip=>$total)

    //echo $databaseClass->getState($zip)." - ".$total."<br>";
    $state = $databaseClass->getState($zip);
    if($state == "")

    echo "";

    else
    echo $state." - ".$total."<br>";
    $running = $running + $total;



    echo $running;


    ?>


    Expected results will be



    New York 267
    California - 3
    etc...



    One total count for each state so I can order them by most popular










    share|improve this question


























      0












      0








      0








      My boss wants to show the most popular states that people request franchising data from. I'm working with zipcodes that users put in. I run it through a function to find the state, but the problem is I have a count for each zip code not a total for the state itself. I need to combine the totals for each zip code(into one total for the state).



      I'm able to add all these up by putting a counter in the loop but it does not break for each state. So I guess the solution would be a way break the loop when the state changes? so currently the output is as such:



      New York - 8
      New York - 11
      New York -236
      New York - 7
      California - 2
      Alabama - 7



      I need to add all of new york into one category.



      <?php
      include("databaseClass.php");
      $databaseClass = new databaseClass();
      $conn = $databaseClass->connect();
      $countFranArr = array();
      $running = 0;


      $sqlFranCount = "select client_zip, count(*) as total from request_form_commits inner join franchise on franchise.franchise_id = request_form_commits.franchise_id
      where time_of_request >= '2011-03-20 18:01:04' and time_of_request <= '2033-03-20 21:03:22' group by client_zip";
      $resultFranCount = $conn->query($sqlFranCount);
      if($resultFranCount->num_rows > 0)

      while($rowFranCount = $resultFranCount->fetch_assoc())

      //array_push_assoc($countCatArr, $rowCatCount['fdd_category'], $rowCatCount['total']);
      //echo $rowCatCount['total']."<br>";
      $countFranArr[$rowFranCount['client_zip']] = $rowFranCount['total'];




      //arsort($countFranArr);

      foreach($countFranArr as $zip=>$total)

      //echo $databaseClass->getState($zip)." - ".$total."<br>";
      $state = $databaseClass->getState($zip);
      if($state == "")

      echo "";

      else
      echo $state." - ".$total."<br>";
      $running = $running + $total;



      echo $running;


      ?>


      Expected results will be



      New York 267
      California - 3
      etc...



      One total count for each state so I can order them by most popular










      share|improve this question














      My boss wants to show the most popular states that people request franchising data from. I'm working with zipcodes that users put in. I run it through a function to find the state, but the problem is I have a count for each zip code not a total for the state itself. I need to combine the totals for each zip code(into one total for the state).



      I'm able to add all these up by putting a counter in the loop but it does not break for each state. So I guess the solution would be a way break the loop when the state changes? so currently the output is as such:



      New York - 8
      New York - 11
      New York -236
      New York - 7
      California - 2
      Alabama - 7



      I need to add all of new york into one category.



      <?php
      include("databaseClass.php");
      $databaseClass = new databaseClass();
      $conn = $databaseClass->connect();
      $countFranArr = array();
      $running = 0;


      $sqlFranCount = "select client_zip, count(*) as total from request_form_commits inner join franchise on franchise.franchise_id = request_form_commits.franchise_id
      where time_of_request >= '2011-03-20 18:01:04' and time_of_request <= '2033-03-20 21:03:22' group by client_zip";
      $resultFranCount = $conn->query($sqlFranCount);
      if($resultFranCount->num_rows > 0)

      while($rowFranCount = $resultFranCount->fetch_assoc())

      //array_push_assoc($countCatArr, $rowCatCount['fdd_category'], $rowCatCount['total']);
      //echo $rowCatCount['total']."<br>";
      $countFranArr[$rowFranCount['client_zip']] = $rowFranCount['total'];




      //arsort($countFranArr);

      foreach($countFranArr as $zip=>$total)

      //echo $databaseClass->getState($zip)." - ".$total."<br>";
      $state = $databaseClass->getState($zip);
      if($state == "")

      echo "";

      else
      echo $state." - ".$total."<br>";
      $running = $running + $total;



      echo $running;


      ?>


      Expected results will be



      New York 267
      California - 3
      etc...



      One total count for each state so I can order them by most popular







      php mysql






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 27 at 18:54









      Dylano236Dylano236

      327 bronze badges




      327 bronze badges

























          1 Answer
          1






          active

          oldest

          votes


















          1















          There are several ways you can do it in your first loop, here's one:



          while($rowFranCount = $resultFranCount->fetch_assoc()) 
          $state = $databaseClass->getState($rowFranCount['client_zip']);

          if(!isset($result[$state]))
          $result[$state] = 0;

          $result[$state] += $rowFranCount['total'];



          Then loop the result to display:



          foreach($result as $state => $total) 
          echo "$state - $total<br>";






          share|improve this answer

























          • You're the man! Or the woman if you're a lady. Did exactly what I need.

            – Dylano236
            Mar 27 at 19:44










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55384596%2fhow-to-add-associative-array-elements-together-grouped-by-their-index%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1















          There are several ways you can do it in your first loop, here's one:



          while($rowFranCount = $resultFranCount->fetch_assoc()) 
          $state = $databaseClass->getState($rowFranCount['client_zip']);

          if(!isset($result[$state]))
          $result[$state] = 0;

          $result[$state] += $rowFranCount['total'];



          Then loop the result to display:



          foreach($result as $state => $total) 
          echo "$state - $total<br>";






          share|improve this answer

























          • You're the man! Or the woman if you're a lady. Did exactly what I need.

            – Dylano236
            Mar 27 at 19:44















          1















          There are several ways you can do it in your first loop, here's one:



          while($rowFranCount = $resultFranCount->fetch_assoc()) 
          $state = $databaseClass->getState($rowFranCount['client_zip']);

          if(!isset($result[$state]))
          $result[$state] = 0;

          $result[$state] += $rowFranCount['total'];



          Then loop the result to display:



          foreach($result as $state => $total) 
          echo "$state - $total<br>";






          share|improve this answer

























          • You're the man! Or the woman if you're a lady. Did exactly what I need.

            – Dylano236
            Mar 27 at 19:44













          1














          1










          1









          There are several ways you can do it in your first loop, here's one:



          while($rowFranCount = $resultFranCount->fetch_assoc()) 
          $state = $databaseClass->getState($rowFranCount['client_zip']);

          if(!isset($result[$state]))
          $result[$state] = 0;

          $result[$state] += $rowFranCount['total'];



          Then loop the result to display:



          foreach($result as $state => $total) 
          echo "$state - $total<br>";






          share|improve this answer













          There are several ways you can do it in your first loop, here's one:



          while($rowFranCount = $resultFranCount->fetch_assoc()) 
          $state = $databaseClass->getState($rowFranCount['client_zip']);

          if(!isset($result[$state]))
          $result[$state] = 0;

          $result[$state] += $rowFranCount['total'];



          Then loop the result to display:



          foreach($result as $state => $total) 
          echo "$state - $total<br>";







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 27 at 19:18









          AbraCadaverAbraCadaver

          62.1k7 gold badges43 silver badges70 bronze badges




          62.1k7 gold badges43 silver badges70 bronze badges















          • You're the man! Or the woman if you're a lady. Did exactly what I need.

            – Dylano236
            Mar 27 at 19:44

















          • You're the man! Or the woman if you're a lady. Did exactly what I need.

            – Dylano236
            Mar 27 at 19:44
















          You're the man! Or the woman if you're a lady. Did exactly what I need.

          – Dylano236
          Mar 27 at 19:44





          You're the man! Or the woman if you're a lady. Did exactly what I need.

          – Dylano236
          Mar 27 at 19:44








          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55384596%2fhow-to-add-associative-array-elements-together-grouped-by-their-index%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

          용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

          155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해