-2

可能重复:
PHP:对多维数组进行排序 ($arr[$i]['v'])

我有一个 UNSORTED VERSION 类型的数组。

$arr['foo']['man']      = 5;
$arr['foo']['str']      = 'string5';
$arr['foobar']['man']   = 2;
$arr['foobar']['str']   = 'string2';
$arr['barfoo']['man']   = 4;
$arr['barfoo']['str']   = 'string4';
$arr['foofoo']['man']   = 1;
$arr['foofoo']['str']   = 'string1';
$arr['bar']['man']      = 3;
$arr['bar']['str']      = 'string3';

我想按降序对它进行排序,同时保留该数组中的所有其他数据,即

$arr['foo']['man']      = 5;
$arr['foo']['str']      = 'string5';
$arr['barfoo']['man']   = 4;
$arr['barfoo']['str']   = 'string4';
$arr['bar']['man']      = 3;
$arr['bar']['str']      = 'string3';
$arr['foobar']['man']   = 2;
$arr['foobar']['str']   = 'string2';
$arr['foofoo']['man']   = 1;
$arr['foofoo']['str']   = 'string1';

我查看了 PHP 中的许多排序函数,但没有找到任何解决方案。

有谁能够帮助我?提前致谢。

4

2 回答 2

2

使用uasort并传递自定义比较可调用:

function compareItems($a, $b) {
    return $b['man'] - $a['man'];
}

uasort($arr, 'compareItems');
于 2012-07-21T14:04:24.780 回答
1
uasort($arr, function($l, $r){
    return $r['man'] - $l['man'];
})
于 2012-07-21T14:04:13.630 回答