How to group this PHP array by date and sum the values [closed]
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
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.
add a comment |
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
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 ofthedate
which becomes the array key, and then apply sum function ontheadults
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
Useusort
to sort your values.
– aldrin27
Nov 18 '15 at 1:37
add a comment |
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
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
php arrays
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 ofthedate
which becomes the array key, and then apply sum function ontheadults
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
Useusort
to sort your values.
– aldrin27
Nov 18 '15 at 1:37
add a comment |
Quite trivial solution is to use value ofthedate
which becomes the array key, and then apply sum function ontheadults
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
Useusort
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
add a comment |
3 Answers
3
active
oldest
votes
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.
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 abovestrlen($element->thedate)
in the conditional to($date = strtotime($element->thedate))
to ensure thatstrtotime()
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
add a comment |
You have:
- an array with
$adults
, where each$adult
is object withthedate
andtheadults
properties.
Now you want to make $groupedAdults
, which will be array using thedate
property as key. So the logic could look like this:
- foreach (
$adults
as$adult
): $date = $adult->thedate
- check if
array_key_exists($date, $groupedAdults)
- if no, copy current
$adult
to$groupedAdults[$date]
- 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.
add a comment |
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);
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
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 abovestrlen($element->thedate)
in the conditional to($date = strtotime($element->thedate))
to ensure thatstrtotime()
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
add a comment |
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.
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 abovestrlen($element->thedate)
in the conditional to($date = strtotime($element->thedate))
to ensure thatstrtotime()
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
add a comment |
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.
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.
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 abovestrlen($element->thedate)
in the conditional to($date = strtotime($element->thedate))
to ensure thatstrtotime()
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
add a comment |
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 abovestrlen($element->thedate)
in the conditional to($date = strtotime($element->thedate))
to ensure thatstrtotime()
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
add a comment |
You have:
- an array with
$adults
, where each$adult
is object withthedate
andtheadults
properties.
Now you want to make $groupedAdults
, which will be array using thedate
property as key. So the logic could look like this:
- foreach (
$adults
as$adult
): $date = $adult->thedate
- check if
array_key_exists($date, $groupedAdults)
- if no, copy current
$adult
to$groupedAdults[$date]
- 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.
add a comment |
You have:
- an array with
$adults
, where each$adult
is object withthedate
andtheadults
properties.
Now you want to make $groupedAdults
, which will be array using thedate
property as key. So the logic could look like this:
- foreach (
$adults
as$adult
): $date = $adult->thedate
- check if
array_key_exists($date, $groupedAdults)
- if no, copy current
$adult
to$groupedAdults[$date]
- 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.
add a comment |
You have:
- an array with
$adults
, where each$adult
is object withthedate
andtheadults
properties.
Now you want to make $groupedAdults
, which will be array using thedate
property as key. So the logic could look like this:
- foreach (
$adults
as$adult
): $date = $adult->thedate
- check if
array_key_exists($date, $groupedAdults)
- if no, copy current
$adult
to$groupedAdults[$date]
- 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.
You have:
- an array with
$adults
, where each$adult
is object withthedate
andtheadults
properties.
Now you want to make $groupedAdults
, which will be array using thedate
property as key. So the logic could look like this:
- foreach (
$adults
as$adult
): $date = $adult->thedate
- check if
array_key_exists($date, $groupedAdults)
- if no, copy current
$adult
to$groupedAdults[$date]
- 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.
answered Nov 18 '15 at 1:40
LihOLihO
32.7k772136
32.7k772136
add a comment |
add a comment |
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);
add a comment |
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);
add a comment |
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);
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);
answered Nov 18 '15 at 2:14
AkshayPAkshayP
1,80321222
1,80321222
add a comment |
add a comment |
Quite trivial solution is to use value of
thedate
which becomes the array key, and then apply sum function ontheadults
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