好的,我有一个用于传输名称的数组,它看起来像这样:
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
对这个数组进行排序?
最佳答案
StackOverflow 有很多类似的问题,但让我举个简单的例子。为此,您可以使用 usort()
功能。
PHP 5.3 示例(不是最好的,但可能更容易理解):
uasort($array, function ($i, $j) {
$a = $i['lastName'];
$b = $j['lastName'];
if ($a == $b) return 0;
elseif ($a > $b) return 1;
else return -1;
});
关于php - 对多维关联数组进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7983822/