I grab data by running a cURL operation and then use:
<?php
$data = json_decode($result, true);
print_r($data)
?>
to output:
Array
(
[0] => Array
(
[id] => 258378365
[firstName] => Test
[lastName] => McTest
[phone] => 1235550101
[email] => test.mctest@example.com
[date] => January 10, 2019
)
[1] => Array
(
[id] => 253994842
[firstName] => Jane
[lastName] => McTest
[phone] => 1235550101
[email] => jane.mctest@example.com
[date] => December 13, 2018
)
[2] => Array
(
[id] => 253994843
[firstName] => Jane
[lastName] => McTest
[phone] => 1235550101
[email] => jane.mctest@example.com
[date] => January 10, 2019
)
)
Is it possible to group my results by date ?
I can display my results like this:
<?php
foreach($data as $entry){
echo $entry['id'];
}
?>
However, I'd like to group my results by $entry['date']
How do I achieve this?
uasort($data, function($a, $b) {
return strtotime($a['date']) > strtotime($b['date']);
});
The following will sort the array by date in ascending order.
usort($data, function ($x,$y) {
return strtotime($x['date']) - strtotime($y['date']);
});
_Edit: I may have misunderstood the question so if the intent is to retrieve only the dates in a separate array then perform the following:
$dates = array_column($data, 'date');
If you needed the dates sorted after this then perform the usort
example above by passing the $dates
array as follows:
usort($dates, function ($x,$y) {
return strtotime($x) - strtotime($y);
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.