如何使用array_multisort()对PHP中的对象数组进行排序?
我正在努力使数组_multisort()正常工作。我正在对从JSON检索到的一些数据进行排序,JSON是一个由五个对象组成的数组,每个对象都包含以下格式的博客文章数据:如何使用array_multisort()对PHP中的对象数组进行排序?,php,arrays,sorting,object,array-multisort,Php,Arrays,Sorting,Object,Array Multisort,我正在努力使数组_multisort()正常工作。我正在对从JSON检索到的一些数据进行排序,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 c
"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" ] },
“1”:{“title”:“它是行星上部的固定器,但我们可以让它工作”,
“发布日期”:“1454889600”,
“作者”:“埃隆·马斯克”,
“内容”:“我们选择在这十年里登上月球,做其他事情……”,
“类别”:[“火星”、“太空旅行”]},
“2”:{“标题”:“失败不是选项”,
“发布日期”:“1456099200”,
“作者”:“Gene Kranz”,
“内容”:“恐龙今天灭绝是因为……”,
“类别”:[“错误引用”,“阿波罗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);
参考请参见下面的代码
$json_content = msort($json_content, "post_date");
And heres the function itself:
/**
* Sort a 2 dimensional array based on 1 or more indexes.
*
* msort() can be used to sort a rowset like array on one or more
* headers (keys in the 2th array).
*
* @param array $array The array to sort.
* @param string|array $key The index(es) to sort the array on.
* @param int $sort_flags The optional parameter to modify the sorting
* behavior. This parameter does not work when
* supplying an array in the $key parameter.
*
* @return array The sorted array.
*/
function msort($array, $key, $sort_flags = SORT_REGULAR) {
if (is_array($array) && count($array) > 0) {
if (!empty($key)) {
$mapping = array();
foreach ($array as $k => $v) {
$sort_key = '';
if (!is_array($key)) {
$sort_key = $v[$key];
} else {
// @TODO This should be fixed, now it will be sorted as string
foreach ($key as $key_key) {
$sort_key .= $v[$key_key];
}
$sort_flags = SORT_STRING;
}
$mapping[$k] = $sort_key;
}
asort($mapping, $sort_flags);
$sorted = array();
foreach ($mapping as $k => $v) {
$sorted[] = $array[$k];
}
return $sorted;
}
}
return $array;
}
有关更多信息,请访问:有关参考信息,请参阅下面的代码
$json_content = msort($json_content, "post_date");
And heres the function itself:
/**
* Sort a 2 dimensional array based on 1 or more indexes.
*
* msort() can be used to sort a rowset like array on one or more
* headers (keys in the 2th array).
*
* @param array $array The array to sort.
* @param string|array $key The index(es) to sort the array on.
* @param int $sort_flags The optional parameter to modify the sorting
* behavior. This parameter does not work when
* supplying an array in the $key parameter.
*
* @return array The sorted array.
*/
function msort($array, $key, $sort_flags = SORT_REGULAR) {
if (is_array($array) && count($array) > 0) {
if (!empty($key)) {
$mapping = array();
foreach ($array as $k => $v) {
$sort_key = '';
if (!is_array($key)) {
$sort_key = $v[$key];
} else {
// @TODO This should be fixed, now it will be sorted as string
foreach ($key as $key_key) {
$sort_key .= $v[$key_key];
}
$sort_flags = SORT_STRING;
}
$mapping[$k] = $sort_key;
}
asort($mapping, $sort_flags);
$sorted = array();
foreach ($mapping as $k => $v) {
$sorted[] = $array[$k];
}
return $sorted;
}
}
return $array;
}
有关更多信息,请访问:编辑:阅读评论后,查看其他有价值的线程,如以下线程:并忽略此处的PHP文档:
我的代码是通过使用索引数组来工作的,array_multisort()排序是提供的第一个数组。同样,传递给数组_multisort()的第一个参数是按排序的,而不是要排序的数组。这与PHP文档相反,但似乎有效。如果您在我的代码中发现对其工作原因的误解或错误,请让我知道。在此之前,我的代码的修复方法是:
数组\u multisort($date\u sort,sort\u DESC,$json\u content)
它似乎按降序对$date\u排序,将最新的日期放在第一位,然后根据第一个对象的排序方式对第二个对象数组进行排序。我想到了像Excel这样的程序如何基于单个列对表进行排序
感谢您抽出时间给我反馈并提出问题。所有这些都有助于最终确定文档的措辞似乎是相反的(因为按排序的数组本身也进行了排序(当然),但这不是调用函数的目的,而是其他数组本身相对于第一个数组进行了排序)。编辑:阅读注释后,检查其他有价值的线程,如以下线程:并忽略此处的PHP文档:
我的代码是通过使用索引数组来工作的,array_multisort()排序是提供的第一个数组。同样,传递给数组_multisort()的第一个参数是按排序的,而不是要排序的数组。这与PHP文档相反,但似乎有效。如果您在我的代码中发现对其工作原因的误解或错误,请让我知道。在此之前,我的代码的修复方法是:
数组\u multisort($date\u sort,sort\u DESC,$json\u content)
它似乎按降序对$date\u排序,将最新的日期放在第一位,然后根据第一个对象的排序方式对第二个对象数组进行排序。我想到了像Excel这样的程序如何基于单个列对表进行排序
感谢您抽出时间给我反馈并提出问题。所有这些都有助于最终确定文档的措辞似乎是相反的(因为按排序的数组本身也进行了排序(当然),但这不是调用函数的目的,而是其他数组本身相对于第一个数组进行排序)。对不起。。你到底想做什么?我想用array_multisort()方法按发布日期排序博客文章,从最新到最旧阅读下面的帖子似乎你已经自己解决了…对吗?是的-我只是在24小时内无法选择它作为答案。非常感谢您的回复:)。对不起。。你到底想做什么?我想用array_multisort()方法按发布日期排序博客文章,从最新到最旧阅读下面的帖子似乎你已经自己解决了…对吗?是的-我只是在24小时内无法选择它作为答案。非常感谢您回来查看:)。为了让它与我的对象数组一起工作,我做了一个小小的更改,那就是更改行$sort\u key.=$v[$key\u key]
至$sort\u key.=$v->$key\u key代码>。谢谢你!为了让它与我的对象数组一起工作,我做了一个小小的更改,那就是更改行$sort\u key.=$v[$key\u key]
至$sort\u key.=$v->$key\u key代码>。谢谢你!