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. I currently print it like this:
...我想按display_name键排序。我目前打印它像这样:
foreach ($blogusers as $bloguser) {
...
}
How do I do this?
我该怎么做呢?
5 个解决方案
#1
34
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)
{
...
#2
11
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;
}
#3
6
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');
#4
0
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附加到查询中。
#5
0
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/
#1
34
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)
{
...
#2
11
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;
}
#3
6
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');
#4
0
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附加到查询中。
#5
0
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/