PHP - 根据另一个键

时间:2017-03-28 04:02:31

标签: php arrays sorting

我有一个数组($ array),其中包含各种值,例如

{"name":"Name1","date":"Mar 27 2017, 01:02:19","comment":"Comment 1","commentid":"48286925"}
{"name":"Name2","date":"Mar 27 2017, 03:55:01","comment":"Comment 2","commentid":"66838672","replytocomment":"33827596"}
{"name":"Name3","date":"Mar 27 2017, 04:49:23","comment":"Comment 3","commentid":"33827596"}

我已经设法通过降序日期将它们按顺序排列。所以$ array [0] ['comment']将是“Comment 1”

正如你所看到的,$ array [2]有一个额外的键“replytocomment”,其值与$ array的“commentid”相同[3]

我想要做的是在匹配项下对数组值进行排序(因为具有匹配“replytocomment”的数组值到另一个数组的“commentid”是对它的回复,它需要在它下面。)

所以$ array的排序顺序是

{"name":"Name1","date":"Mar 27 2017, 01:02:19","comment":"Comment 1","commentid":"48286925"}
{"name":"Name3","date":"Mar 27 2017, 04:49:23","comment":"Comment 3","commentid":"33827596"}
{"name":"Name2","date":"Mar 27 2017, 03:55:01","comment":"Comment 2","commentid":"66838672","replytocomment":"33827596"}

忽略“响应”的时间戳比响应时更​​新的事实,这只是我编写的一些简化示例行

0 个答案:

没有答案