How to group this PHP array by date and sum the values [closed]










0















I have this PHP array - I need to group by date, I am assuming by creating another array, and add up the number of adults for each date



Array
(
[0] => stdClass Object
(
[thedate] => 2016-04-09
[theadults] => 5
)

[1] => stdClass Object
(
[thedate] => 2016-04-09
[theadults] => 8
)

[2] => stdClass Object
(
[thedate] => 2016-04-09
[theadults] => 12
)

[3] => stdClass Object
(
[thedate] => 2016-03-05
[theadults] => 1
)

[4] => stdClass Object
(
[thedate] => 2016-02-27
[theadults] => 1
)

[5] => stdClass Object
(
[thedate] => 2016-03-05
[theadults] => 1
)

[6] => stdClass Object
(
[thedate] => 2016-03-05
[theadults] => 1
)

[7] => stdClass Object
(
[thedate] => 2016-04-09
[theadults] => 3
)

)


So the desired output would be like this (also ordering by date):



Array
(
[0] => stdClass Object
(
[thedate] => 2016-02-27
[theadults] => 1
)

[1] => stdClass Object
(
[thedate] => 2016-03-05
[theadults] => 3
)

[2] => stdClass Object
(
[thedate] => 2016-04-09
[theadults] => 24
)

)


Could anyone tell me the most efficient way to accomplish this?
Thanks in advance for any help gratefully received.










share|improve this question













closed as too broad by John Conde, Ian Stapleton Cordasco, Matt Clark, Rajesh, Jonathan Lam Nov 18 '15 at 5:40


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.


















  • Quite trivial solution is to use value of thedate which becomes the array key, and then apply sum function on theadults value, e.g. $arr['2016-03-05'] = 1; and so on. There are multiple good solutions available here.

    – jpaljasma
    Nov 18 '15 at 1:34











  • Use usort to sort your values.

    – aldrin27
    Nov 18 '15 at 1:37















0















I have this PHP array - I need to group by date, I am assuming by creating another array, and add up the number of adults for each date



Array
(
[0] => stdClass Object
(
[thedate] => 2016-04-09
[theadults] => 5
)

[1] => stdClass Object
(
[thedate] => 2016-04-09
[theadults] => 8
)

[2] => stdClass Object
(
[thedate] => 2016-04-09
[theadults] => 12
)

[3] => stdClass Object
(
[thedate] => 2016-03-05
[theadults] => 1
)

[4] => stdClass Object
(
[thedate] => 2016-02-27
[theadults] => 1
)

[5] => stdClass Object
(
[thedate] => 2016-03-05
[theadults] => 1
)

[6] => stdClass Object
(
[thedate] => 2016-03-05
[theadults] => 1
)

[7] => stdClass Object
(
[thedate] => 2016-04-09
[theadults] => 3
)

)


So the desired output would be like this (also ordering by date):



Array
(
[0] => stdClass Object
(
[thedate] => 2016-02-27
[theadults] => 1
)

[1] => stdClass Object
(
[thedate] => 2016-03-05
[theadults] => 3
)

[2] => stdClass Object
(
[thedate] => 2016-04-09
[theadults] => 24
)

)


Could anyone tell me the most efficient way to accomplish this?
Thanks in advance for any help gratefully received.










share|improve this question













closed as too broad by John Conde, Ian Stapleton Cordasco, Matt Clark, Rajesh, Jonathan Lam Nov 18 '15 at 5:40


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.


















  • Quite trivial solution is to use value of thedate which becomes the array key, and then apply sum function on theadults value, e.g. $arr['2016-03-05'] = 1; and so on. There are multiple good solutions available here.

    – jpaljasma
    Nov 18 '15 at 1:34











  • Use usort to sort your values.

    – aldrin27
    Nov 18 '15 at 1:37













0












0








0








I have this PHP array - I need to group by date, I am assuming by creating another array, and add up the number of adults for each date



Array
(
[0] => stdClass Object
(
[thedate] => 2016-04-09
[theadults] => 5
)

[1] => stdClass Object
(
[thedate] => 2016-04-09
[theadults] => 8
)

[2] => stdClass Object
(
[thedate] => 2016-04-09
[theadults] => 12
)

[3] => stdClass Object
(
[thedate] => 2016-03-05
[theadults] => 1
)

[4] => stdClass Object
(
[thedate] => 2016-02-27
[theadults] => 1
)

[5] => stdClass Object
(
[thedate] => 2016-03-05
[theadults] => 1
)

[6] => stdClass Object
(
[thedate] => 2016-03-05
[theadults] => 1
)

[7] => stdClass Object
(
[thedate] => 2016-04-09
[theadults] => 3
)

)


So the desired output would be like this (also ordering by date):



Array
(
[0] => stdClass Object
(
[thedate] => 2016-02-27
[theadults] => 1
)

[1] => stdClass Object
(
[thedate] => 2016-03-05
[theadults] => 3
)

[2] => stdClass Object
(
[thedate] => 2016-04-09
[theadults] => 24
)

)


Could anyone tell me the most efficient way to accomplish this?
Thanks in advance for any help gratefully received.










share|improve this question














I have this PHP array - I need to group by date, I am assuming by creating another array, and add up the number of adults for each date



Array
(
[0] => stdClass Object
(
[thedate] => 2016-04-09
[theadults] => 5
)

[1] => stdClass Object
(
[thedate] => 2016-04-09
[theadults] => 8
)

[2] => stdClass Object
(
[thedate] => 2016-04-09
[theadults] => 12
)

[3] => stdClass Object
(
[thedate] => 2016-03-05
[theadults] => 1
)

[4] => stdClass Object
(
[thedate] => 2016-02-27
[theadults] => 1
)

[5] => stdClass Object
(
[thedate] => 2016-03-05
[theadults] => 1
)

[6] => stdClass Object
(
[thedate] => 2016-03-05
[theadults] => 1
)

[7] => stdClass Object
(
[thedate] => 2016-04-09
[theadults] => 3
)

)


So the desired output would be like this (also ordering by date):



Array
(
[0] => stdClass Object
(
[thedate] => 2016-02-27
[theadults] => 1
)

[1] => stdClass Object
(
[thedate] => 2016-03-05
[theadults] => 3
)

[2] => stdClass Object
(
[thedate] => 2016-04-09
[theadults] => 24
)

)


Could anyone tell me the most efficient way to accomplish this?
Thanks in advance for any help gratefully received.







php arrays






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 18 '15 at 1:29









Shaun StevensShaun Stevens

306




306




closed as too broad by John Conde, Ian Stapleton Cordasco, Matt Clark, Rajesh, Jonathan Lam Nov 18 '15 at 5:40


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.









closed as too broad by John Conde, Ian Stapleton Cordasco, Matt Clark, Rajesh, Jonathan Lam Nov 18 '15 at 5:40


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.














  • Quite trivial solution is to use value of thedate which becomes the array key, and then apply sum function on theadults value, e.g. $arr['2016-03-05'] = 1; and so on. There are multiple good solutions available here.

    – jpaljasma
    Nov 18 '15 at 1:34











  • Use usort to sort your values.

    – aldrin27
    Nov 18 '15 at 1:37

















  • Quite trivial solution is to use value of thedate which becomes the array key, and then apply sum function on theadults value, e.g. $arr['2016-03-05'] = 1; and so on. There are multiple good solutions available here.

    – jpaljasma
    Nov 18 '15 at 1:34











  • Use usort to sort your values.

    – aldrin27
    Nov 18 '15 at 1:37
















Quite trivial solution is to use value of thedate which becomes the array key, and then apply sum function on theadults value, e.g. $arr['2016-03-05'] = 1; and so on. There are multiple good solutions available here.

– jpaljasma
Nov 18 '15 at 1:34





Quite trivial solution is to use value of thedate which becomes the array key, and then apply sum function on theadults value, e.g. $arr['2016-03-05'] = 1; and so on. There are multiple good solutions available here.

– jpaljasma
Nov 18 '15 at 1:34













Use usort to sort your values.

– aldrin27
Nov 18 '15 at 1:37





Use usort to sort your values.

– aldrin27
Nov 18 '15 at 1:37












3 Answers
3






active

oldest

votes


















2














Looks like the simplest way would to be to aggregate the counts into an array with the date as the key. You could then sort on the key and output the data however you desired.



Consider this:



$totalsByDate = ;
foreach ($array as $element) if (($date = strtotime($element->thedate)) and is_numeric($element->theadults))
$totalsByDate[$date] += $element->theadults;

ksort($totalsByDate);
foreach ($totalsByDate as $date => $total)
echo "On " . date("Y-m-d", $date) . " there were " . $total " adults.n";



You could also replace the ksort() with krsort() if you wanted the dates sorted in descending order.






share|improve this answer

























  • thank you very much, this is very concise and good logic

    – Shaun Stevens
    Nov 18 '15 at 10:44











  • You're very welcome. After taking a second look, I changed the above strlen($element->thedate) in the conditional to ($date = strtotime($element->thedate)) to ensure that strtotime() is actually able to parse the date (as opposed to just checking to make sure it's there) before letting the assignment take place.

    – EVILoptimist
    Nov 18 '15 at 18:19











  • thanks again! Perfect.

    – Shaun Stevens
    Nov 19 '15 at 12:42


















0














You have:

  • an array with $adults, where each $adult is object with thedate and theadults properties.

Now you want to make $groupedAdults, which will be array using thedate property as key. So the logic could look like this:



  1. foreach ($adults as $adult):

  2. $date = $adult->thedate

  3. check if array_key_exists($date, $groupedAdults)

  4. if no, copy current $adult to $groupedAdults[$date]

  5. if yes, add $adult->theadults to the $groupedAdults[$date]->adults

Give it a try, hopefully you will be able to write it on your own now.






share|improve this answer






























    0














    Try something like below



    <?php

    $array= array(
    array("thedate"=>"2016-03-05","theadults"=>10),
    array("thedate"=>"2016-03-05","theadults"=>1),
    array("thedate"=>"2016-03-06","theadults"=>1),
    array("thedate"=>"2016-03-07","theadults"=>1),
    array("thedate"=>"2016-03-05","theadults"=>1),
    array("thedate"=>"2016-03-07","theadults"=>1),
    array("thedate"=>"2016-03-06","theadults"=>1)
    );

    print_r($array);
    $new = array();
    for($i=0;$i < count($array);$i++)

    $index = -1;
    for($j=0;$j<count($new);$j++)

    if($array[$i]['thedate'] == $new[$j]['thedate'])

    $index = $j;
    breck;


    if($index == -1)

    array_push($new, $array[$i]);

    else

    $new[$index]['theadults'] += $array[$i]['theadults'];


    echo "<hr>";
    print_r($new);





    share|improve this answer





























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      Looks like the simplest way would to be to aggregate the counts into an array with the date as the key. You could then sort on the key and output the data however you desired.



      Consider this:



      $totalsByDate = ;
      foreach ($array as $element) if (($date = strtotime($element->thedate)) and is_numeric($element->theadults))
      $totalsByDate[$date] += $element->theadults;

      ksort($totalsByDate);
      foreach ($totalsByDate as $date => $total)
      echo "On " . date("Y-m-d", $date) . " there were " . $total " adults.n";



      You could also replace the ksort() with krsort() if you wanted the dates sorted in descending order.






      share|improve this answer

























      • thank you very much, this is very concise and good logic

        – Shaun Stevens
        Nov 18 '15 at 10:44











      • You're very welcome. After taking a second look, I changed the above strlen($element->thedate) in the conditional to ($date = strtotime($element->thedate)) to ensure that strtotime() is actually able to parse the date (as opposed to just checking to make sure it's there) before letting the assignment take place.

        – EVILoptimist
        Nov 18 '15 at 18:19











      • thanks again! Perfect.

        – Shaun Stevens
        Nov 19 '15 at 12:42















      2














      Looks like the simplest way would to be to aggregate the counts into an array with the date as the key. You could then sort on the key and output the data however you desired.



      Consider this:



      $totalsByDate = ;
      foreach ($array as $element) if (($date = strtotime($element->thedate)) and is_numeric($element->theadults))
      $totalsByDate[$date] += $element->theadults;

      ksort($totalsByDate);
      foreach ($totalsByDate as $date => $total)
      echo "On " . date("Y-m-d", $date) . " there were " . $total " adults.n";



      You could also replace the ksort() with krsort() if you wanted the dates sorted in descending order.






      share|improve this answer

























      • thank you very much, this is very concise and good logic

        – Shaun Stevens
        Nov 18 '15 at 10:44











      • You're very welcome. After taking a second look, I changed the above strlen($element->thedate) in the conditional to ($date = strtotime($element->thedate)) to ensure that strtotime() is actually able to parse the date (as opposed to just checking to make sure it's there) before letting the assignment take place.

        – EVILoptimist
        Nov 18 '15 at 18:19











      • thanks again! Perfect.

        – Shaun Stevens
        Nov 19 '15 at 12:42













      2












      2








      2







      Looks like the simplest way would to be to aggregate the counts into an array with the date as the key. You could then sort on the key and output the data however you desired.



      Consider this:



      $totalsByDate = ;
      foreach ($array as $element) if (($date = strtotime($element->thedate)) and is_numeric($element->theadults))
      $totalsByDate[$date] += $element->theadults;

      ksort($totalsByDate);
      foreach ($totalsByDate as $date => $total)
      echo "On " . date("Y-m-d", $date) . " there were " . $total " adults.n";



      You could also replace the ksort() with krsort() if you wanted the dates sorted in descending order.






      share|improve this answer















      Looks like the simplest way would to be to aggregate the counts into an array with the date as the key. You could then sort on the key and output the data however you desired.



      Consider this:



      $totalsByDate = ;
      foreach ($array as $element) if (($date = strtotime($element->thedate)) and is_numeric($element->theadults))
      $totalsByDate[$date] += $element->theadults;

      ksort($totalsByDate);
      foreach ($totalsByDate as $date => $total)
      echo "On " . date("Y-m-d", $date) . " there were " . $total " adults.n";



      You could also replace the ksort() with krsort() if you wanted the dates sorted in descending order.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 18 '15 at 18:20

























      answered Nov 18 '15 at 1:45









      EVILoptimistEVILoptimist

      807




      807












      • thank you very much, this is very concise and good logic

        – Shaun Stevens
        Nov 18 '15 at 10:44











      • You're very welcome. After taking a second look, I changed the above strlen($element->thedate) in the conditional to ($date = strtotime($element->thedate)) to ensure that strtotime() is actually able to parse the date (as opposed to just checking to make sure it's there) before letting the assignment take place.

        – EVILoptimist
        Nov 18 '15 at 18:19











      • thanks again! Perfect.

        – Shaun Stevens
        Nov 19 '15 at 12:42

















      • thank you very much, this is very concise and good logic

        – Shaun Stevens
        Nov 18 '15 at 10:44











      • You're very welcome. After taking a second look, I changed the above strlen($element->thedate) in the conditional to ($date = strtotime($element->thedate)) to ensure that strtotime() is actually able to parse the date (as opposed to just checking to make sure it's there) before letting the assignment take place.

        – EVILoptimist
        Nov 18 '15 at 18:19











      • thanks again! Perfect.

        – Shaun Stevens
        Nov 19 '15 at 12:42
















      thank you very much, this is very concise and good logic

      – Shaun Stevens
      Nov 18 '15 at 10:44





      thank you very much, this is very concise and good logic

      – Shaun Stevens
      Nov 18 '15 at 10:44













      You're very welcome. After taking a second look, I changed the above strlen($element->thedate) in the conditional to ($date = strtotime($element->thedate)) to ensure that strtotime() is actually able to parse the date (as opposed to just checking to make sure it's there) before letting the assignment take place.

      – EVILoptimist
      Nov 18 '15 at 18:19





      You're very welcome. After taking a second look, I changed the above strlen($element->thedate) in the conditional to ($date = strtotime($element->thedate)) to ensure that strtotime() is actually able to parse the date (as opposed to just checking to make sure it's there) before letting the assignment take place.

      – EVILoptimist
      Nov 18 '15 at 18:19













      thanks again! Perfect.

      – Shaun Stevens
      Nov 19 '15 at 12:42





      thanks again! Perfect.

      – Shaun Stevens
      Nov 19 '15 at 12:42













      0














      You have:

      • an array with $adults, where each $adult is object with thedate and theadults properties.

      Now you want to make $groupedAdults, which will be array using thedate property as key. So the logic could look like this:



      1. foreach ($adults as $adult):

      2. $date = $adult->thedate

      3. check if array_key_exists($date, $groupedAdults)

      4. if no, copy current $adult to $groupedAdults[$date]

      5. if yes, add $adult->theadults to the $groupedAdults[$date]->adults

      Give it a try, hopefully you will be able to write it on your own now.






      share|improve this answer



























        0














        You have:

        • an array with $adults, where each $adult is object with thedate and theadults properties.

        Now you want to make $groupedAdults, which will be array using thedate property as key. So the logic could look like this:



        1. foreach ($adults as $adult):

        2. $date = $adult->thedate

        3. check if array_key_exists($date, $groupedAdults)

        4. if no, copy current $adult to $groupedAdults[$date]

        5. if yes, add $adult->theadults to the $groupedAdults[$date]->adults

        Give it a try, hopefully you will be able to write it on your own now.






        share|improve this answer

























          0












          0








          0







          You have:

          • an array with $adults, where each $adult is object with thedate and theadults properties.

          Now you want to make $groupedAdults, which will be array using thedate property as key. So the logic could look like this:



          1. foreach ($adults as $adult):

          2. $date = $adult->thedate

          3. check if array_key_exists($date, $groupedAdults)

          4. if no, copy current $adult to $groupedAdults[$date]

          5. if yes, add $adult->theadults to the $groupedAdults[$date]->adults

          Give it a try, hopefully you will be able to write it on your own now.






          share|improve this answer













          You have:

          • an array with $adults, where each $adult is object with thedate and theadults properties.

          Now you want to make $groupedAdults, which will be array using thedate property as key. So the logic could look like this:



          1. foreach ($adults as $adult):

          2. $date = $adult->thedate

          3. check if array_key_exists($date, $groupedAdults)

          4. if no, copy current $adult to $groupedAdults[$date]

          5. if yes, add $adult->theadults to the $groupedAdults[$date]->adults

          Give it a try, hopefully you will be able to write it on your own now.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 18 '15 at 1:40









          LihOLihO

          32.7k772136




          32.7k772136





















              0














              Try something like below



              <?php

              $array= array(
              array("thedate"=>"2016-03-05","theadults"=>10),
              array("thedate"=>"2016-03-05","theadults"=>1),
              array("thedate"=>"2016-03-06","theadults"=>1),
              array("thedate"=>"2016-03-07","theadults"=>1),
              array("thedate"=>"2016-03-05","theadults"=>1),
              array("thedate"=>"2016-03-07","theadults"=>1),
              array("thedate"=>"2016-03-06","theadults"=>1)
              );

              print_r($array);
              $new = array();
              for($i=0;$i < count($array);$i++)

              $index = -1;
              for($j=0;$j<count($new);$j++)

              if($array[$i]['thedate'] == $new[$j]['thedate'])

              $index = $j;
              breck;


              if($index == -1)

              array_push($new, $array[$i]);

              else

              $new[$index]['theadults'] += $array[$i]['theadults'];


              echo "<hr>";
              print_r($new);





              share|improve this answer



























                0














                Try something like below



                <?php

                $array= array(
                array("thedate"=>"2016-03-05","theadults"=>10),
                array("thedate"=>"2016-03-05","theadults"=>1),
                array("thedate"=>"2016-03-06","theadults"=>1),
                array("thedate"=>"2016-03-07","theadults"=>1),
                array("thedate"=>"2016-03-05","theadults"=>1),
                array("thedate"=>"2016-03-07","theadults"=>1),
                array("thedate"=>"2016-03-06","theadults"=>1)
                );

                print_r($array);
                $new = array();
                for($i=0;$i < count($array);$i++)

                $index = -1;
                for($j=0;$j<count($new);$j++)

                if($array[$i]['thedate'] == $new[$j]['thedate'])

                $index = $j;
                breck;


                if($index == -1)

                array_push($new, $array[$i]);

                else

                $new[$index]['theadults'] += $array[$i]['theadults'];


                echo "<hr>";
                print_r($new);





                share|improve this answer

























                  0












                  0








                  0







                  Try something like below



                  <?php

                  $array= array(
                  array("thedate"=>"2016-03-05","theadults"=>10),
                  array("thedate"=>"2016-03-05","theadults"=>1),
                  array("thedate"=>"2016-03-06","theadults"=>1),
                  array("thedate"=>"2016-03-07","theadults"=>1),
                  array("thedate"=>"2016-03-05","theadults"=>1),
                  array("thedate"=>"2016-03-07","theadults"=>1),
                  array("thedate"=>"2016-03-06","theadults"=>1)
                  );

                  print_r($array);
                  $new = array();
                  for($i=0;$i < count($array);$i++)

                  $index = -1;
                  for($j=0;$j<count($new);$j++)

                  if($array[$i]['thedate'] == $new[$j]['thedate'])

                  $index = $j;
                  breck;


                  if($index == -1)

                  array_push($new, $array[$i]);

                  else

                  $new[$index]['theadults'] += $array[$i]['theadults'];


                  echo "<hr>";
                  print_r($new);





                  share|improve this answer













                  Try something like below



                  <?php

                  $array= array(
                  array("thedate"=>"2016-03-05","theadults"=>10),
                  array("thedate"=>"2016-03-05","theadults"=>1),
                  array("thedate"=>"2016-03-06","theadults"=>1),
                  array("thedate"=>"2016-03-07","theadults"=>1),
                  array("thedate"=>"2016-03-05","theadults"=>1),
                  array("thedate"=>"2016-03-07","theadults"=>1),
                  array("thedate"=>"2016-03-06","theadults"=>1)
                  );

                  print_r($array);
                  $new = array();
                  for($i=0;$i < count($array);$i++)

                  $index = -1;
                  for($j=0;$j<count($new);$j++)

                  if($array[$i]['thedate'] == $new[$j]['thedate'])

                  $index = $j;
                  breck;


                  if($index == -1)

                  array_push($new, $array[$i]);

                  else

                  $new[$index]['theadults'] += $array[$i]['theadults'];


                  echo "<hr>";
                  print_r($new);






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 18 '15 at 2:14









                  AkshayPAkshayP

                  1,80321222




                  1,80321222













                      Popular posts from this blog

                      Top Tejano songwriter Luis Silva dead of heart attack at 64

                      ReactJS Fetched API data displays live - need Data displayed static

                      政党