[英]Sort multi-dimensional array by specific key
I have an array: 我有一个数组:
Array (
[0] => stdClass Object (
[user_id] => 1
[ID] => 1
[user_login] => admin
[display_name] => admin
[user_email] => webbinformation@nacka.se
[meta_value] => a:1:{s:13:\"administrator\";s:1:\"1\";}
)
[1] => stdClass Object (
[user_id] => 4
[ID] => 4
[user_login] => ungtinflytande
[display_name] => ungtinflytande
[user_email] => klarakviberg@gmail.com
[meta_value] => a:1:{s:13:\"administrator\";s:1:\"1\";}
)
[2] => stdClass Object (
[user_id] => 5
[ID] => 5
[user_login] => inflytandepilot
[display_name] => inflytandepilot
[user_email] => hildalundgren@hotmail.com
[meta_value] => a:1:{s:6:\"author\";s:1:\"1\";}
)
[3] => stdClass Object (
[user_id] => 11
[ID] => 11
[user_login] => matsbohman
[display_name] => matsbohman
[user_email] => mats.bohman@nacka.se
[meta_value] => a:1:{s:6:\"editor\";s:1:\"1\";}
)
[4] => stdClass Object (
[user_id] => 12
[ID] => 12
[user_login] => klarakviberg
[display_name] => klarakviberg
[user_email] => nastasteg@nacka.se
[meta_value] => a:1:{s:13:\"administrator\";s:1:\"1\";}
)
)
...that I wanna sort by the display_name
key. ...我想按display_name
键排序。 I currently print it like this: 我目前打印它像这样:
foreach ($blogusers as $bloguser) {
...
}
How do I do this? 我该怎么做呢?
You would use usort() - http://php.net/usort 你会使用usort() - http://php.net/usort
My suggestion would be: 我的建议是:
function cmp($a, $b)
{
return strcmp($a->display_name, $b->display_name);
}
usort($blogusers, "cmp");
foreach ($blogusers as $bloguser)
{
...
See usort: http://php.net/manual/en/function.usort.php 见usort: http : //php.net/manual/en/function.usort.php
usort($array, "my_cmp");
function my_cmp($a, $b) {
if ($a->display_name == $b->display_name) {
return 0;
}
return ($a->display_name < $b->display_name) ? -1 : 1;
}
I have find answer at https://joshtronic.com/2013/09/23/sorting-associative-array-specific-key/ 我在https://joshtronic.com/2013/09/23/sorting-associative-array-specific-key/找到答案
function sortBy($field, &$array, $direction = 'asc')
{
usort($array, create_function('$a, $b', '
$a = $a["' . $field . '"];
$b = $b["' . $field . '"];
if ($a == $b)
{
return 0;
}
return ($a ' . ($direction == 'desc' ? '>' : '<') .' $b) ? -1 : 1;
'));
return true;
}
And now call this function by specific array key. 现在通过特定的数组键调用此函数。
$newArray = sortBy('display_name', $blogusers);
And if sort in asc/desc just add one argument, 如果在asc / desc中排序只需添加一个参数,
sortBy('display_name', $blogusers, 'desc');
Take a look at following article. 看看下面的文章。 It does describe how to use usort()
and also describes how to use create_function()
so that you can use single function to sort on different fields (with required direction asc
or desc
). 它确实描述了如何使用usort()
并且还描述了如何使用create_function()
以便您可以使用单个函数对不同的字段(具有所需的方向asc
或desc
)进行排序。
http://phpave.com/sorting-associative-array-specific-key/ http://phpave.com/sorting-associative-array-specific-key/
Your array looks like the result of a database query. 您的数组看起来像是数据库查询的结果。 If this is a case, let the database do the sorting: just append ORDER BY display_name
to the query. 如果是这种情况,请让数据库进行排序:只需将ORDER BY display_name
附加到查询中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.