【发布时间】:2017-05-16 05:11:19
【问题描述】:
我正在努力让 array_multisort() 工作。我正在对从 JSON 检索到的一些数据进行排序,这是一个由五个对象组成的数组,每个对象都包含这种格式的博客文章数据:
"1":{"title": "It's a fixer-upper of a planet but we could make it work",
"post_date": "1454889600",
"author": "Elon Musk",
"content": "<p>We choose to go to the moon in this decade and do the other things...</p>",
"category": [ "mars", "space travel" ] },
"2":{"title": "Failure is not an option",
"post_date": "1456099200",
"author": "Gene Kranz",
"content": "<p>Dinosaurs are extinct today because ...</p>",
"category": [ "mis-quoted", "apollo 13" ] },
...等
我在 PHP 中获取文件,将 JSON 解码为关联数组,然后创建我正在工作的人类可读日期数组。我有一个由五个对象组成的数组,需要按所述日期对数组进行排序。然后我尝试使用 array_multisort 并且似乎找不到有效的语法。任何帮助将不胜感激,我相信这是我忽略的小事。无论我多么努力地用谷歌搜索,我似乎都无法正确找到搜索字符串。请帮忙?
<?php //This part I'm confident is working.
$json = file_get_contents("./data/posts.json");
$json_content = json_decode($json, true);
$date_sort = array ();
//Sorting the Array - this part seems to work
foreach ($json_content as $postObj) {
$post_date_human = date ('Y-m-d', $postObj['post_date']);
array_push($date_sort, $post_date_human);
}
print_r ($date_sort); //Seems to be working fine, now to try to sort one array of objects by the position of dates in the second array
// Wai u no werk!?
array_multisort($json_content, $date_sort = SORT_ASC);
print_r ($json_content);
【问题讨论】:
-
对不起.. 你真正想做什么?
-
我想使用 array_multisort() 方法按发布日期排序博客文章,从最新到最旧。 :)
-
看你下面的帖子好像你自己解决了...对吧?
-
是的 - 我只是无法选择它作为 24 小时的答案。非常感谢您回来查看:)。
标签: php arrays sorting object array-multisort