排序多维关联数组? [英] sort a multi-dimensional associative array?
本文介绍了排序多维关联数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
好的,我有一个用于传输名称的数组,它看起来像这样:
Okay, I have an array that is used to transport names, it looks like this:
array(2) {
[0]=>
array(3) {
["firstName"]=>
string(3) "Joe"
["lastName"]=>
string(5) "Black"
["uid"]=>
int(3225)
}
[1]=>
array(3) {
["firstName"]=>
string(4) "John"
["lastName"]=>
string(3) "Doe"
["uid"]=>
int(3516)
}
}
现在,如何按lastName
对该数组排序?
Now, how do I sort this array by lastName
?
推荐答案
StackOverflow有很多类似的问题,但让我举一个简单的例子.为此,您可以使用 usort()
函数.
StackOverflow has lots of similar questions, but let me give you a quick example. For this, you can use the usort()
function.
PHP 5.3示例(不是最好的示例,但可能更容易理解):
PHP 5.3 example (not the nicest one, but might be easier to understand):
uasort($array, function ($i, $j) {
$a = $i['lastName'];
$b = $j['lastName'];
if ($a == $b) return 0;
elseif ($a > $b) return 1;
else return -1;
});
这篇关于排序多维关联数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文