我通过运行cURL操作来获取数据,然后使用:
<?php
$data = json_decode($result, true);
print_r($data)
?>
输出:
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
)
)
是否可以按日期对结果进行分组?
我可以这样显示我的结果:
<?php
foreach($data as $entry){
echo $entry['id'];
}
?>
但是,我想按$entry['date']
我该如何实现?
答案 0 :(得分:0)
uasort($data, function($a, $b) {
return strtotime($a['date']) > strtotime($b['date']);
});
答案 1 :(得分:0)
以下内容将按日期升序对数组进行排序。
usort($data, function ($x,$y) {
return strtotime($x['date']) - strtotime($y['date']);
});
_Edit:我可能误解了这个问题,所以如果目的是仅检索单独数组中的日期,请执行以下操作:
$dates = array_column($data, 'date');
如果您需要在此之后排序的日期,则通过传递usort
数组,以如下方式执行上面的$dates
示例:
usort($dates, function ($x,$y) {
return strtotime($x) - strtotime($y);
});