【发布时间】:2019-01-12 04:41:43
【问题描述】:
我有以下多个一维关联数组的二维索引数组:
Array (
[0] => Array (
[from] => Person 1
[to] => Person 2
[platform] => Instagram Direct Messaging
[date] => 2016/06/27
[time] => 12:00
[ampm] => PM
[specialcontent] => none
[content] => Hello
)
[1] => Array (
[from] => Person 1
[to] => Person 2
[platform] => Instagram Direct Messaging
[date] => 2016/06/27
[time] => 12:00
[ampm] => PM
[specialcontent] => none
[content] => How are you?
)
[2] => Array (
[from] => Person 2
[to] => Person 1
[platform] => Instagram Direct Messaging
[date] => 2016/06/27
[time] => 6:00
[ampm] => PM
[specialcontent] => none
[content] => Oh, hey there. I'm fine
)
[3] => Array (
[from] => Person 2
[to] => Person 1
[platform] => Instagram Direct Messaging
[date] => 2016/06/27
[time] => 6:01
[ampm] => PM
[specialcontent] => none
[content] => What about you?
)
)
我想按其中的date 字段(键)的值对内部数组进行排序,这意味着,我必须按年、月、日对它们进行升序排序命令。我可以分别使用以下命令从date(键)获取这些值:$year = substr($item['date'], 0, 4);、$month = substr($item['date'], 5, 2);、$day = substr($item['date'], -2);。我也知道我可能需要使用usort 或array_multisort 之类的函数,但我想不出如何创建一个函数来按年、月、日返回数组的顺序@ 987654329@键。
【问题讨论】:
-
顺便说一句,它们已经在我的代码中订购了。假设它们是无序的。
-
不行吗?
usort($arr, function ($a, $b) { return $a['date'] <=> $b['date']; }); -
@AntoineB 如果 Nikita U. 的评论有效,我认为这是重复的,我现在将对其进行测试。然后我会评论结果。
-
@NikitaU。我不知道 PHP 是如何做到的,但它确实有效。那么,我应该删除这个问题吗?我已经得到了答案(谢谢你),但没有人回答。也许你可以用那个评论来回答,我会选择你的答案。
-
@Machavity 这与你所说的重复的问题不太一样。