我很努力让array_multisort()工作。我从排序检索JSON一些数据,是五个对象,每个这种格式的博客文章一组数据:如何使用array_multisort()对PHP中的对象数组进行排序?
"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);
对不起。你真的想做什么? – Nidhi
我想使用array_multisort()方法按发布日期排序博客文章,最近到最早。 :) –
在下面阅读你的文章看起来像你自己解决了...对吗? – Nidhi