我从几个数据库中提取了一堆内容,并编译成一个嵌套数组,如下所示:
Array
(
[0] => Array
(
[id] => 3
[published] => 1433940002
[content] => This is some content
)
[1] => Array
(
[id] => 52
[published] => 1433940001
[content] => This is some more content
)
[2] => Array
(
[id] => 16
[published] => 1433940003
[content] => This is some more content
)
)
由于我无法在检索内容时对内容进行排序(因为它是使用来自尽可能多的数据库的多个查询完成的),我想在数组中挖掘一个级别并按"发布"日期排序,同时保持数组的深度,所以我最终得到......
Array
(
[0] => Array
(
[id] => 52
[published] => 1433940001
[content] => This is some more content
)
[1] => Array
(
[id] => 3
[published] => 1433940002
[content] => This is some content
)
[2] => Array
(
[id] => 16
[published] => 1433940003
[content] => This is some more content
)
)
我很确定array_multisort()是要走的路,但我想不出从哪里开始!有人能好心地给我指点吗?
多亏了
@Rizier123对我问题的监督,我自己想通了。对于找到此线程的任何其他人,这是我使用上面提供的示例数组的解决方案。
//-- get disorganised content from databases here
usort($array, "publishcmp");
//-- output organised content here
function publishcmp($a , $b){
if ($a['published'] == $b['published']) {
return 0;
}
return ($a['published'] < $b['published']) ? -1 : 1;
}
几乎逐字逐句摘自 php.net