php 按特定键对多维数组进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4022289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sort multi-dimensional array by specific key
提问by Himmators
I have an array:
我有一个数组:
Array (
[0] => stdClass Object (
[user_id] => 1
[ID] => 1
[user_login] => admin
[display_name] => admin
[user_email] => [email protected]
[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] => [email protected]
[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] => [email protected]
[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] => [email protected]
[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] => [email protected]
[meta_value] => a:1:{s:13:\"administrator\";s:1:\"1\";}
)
)
...that I wanna sort by the display_name
key. I currently print it like this:
...我想display_name
按键排序。我目前这样打印:
foreach ($blogusers as $bloguser) {
...
}
How do I do this?
我该怎么做呢?
回答by Repox
You would use usort() - http://php.net/usort
你会使用 usort() - http://php.net/usor
My suggestion would be:
我的建议是:
function cmp($a, $b)
{
return strcmp($a->display_name, $b->display_name);
}
usort($blogusers, "cmp");
foreach ($blogusers as $bloguser)
{
...
回答by Mike C
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;
}
回答by Kishan Chauhan
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');
回答by Shriganesh Shintre
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
)。
回答by ax.
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
到查询。