0

我的数组类型为(未排序的版本):

<?php
$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' ;
?>

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

<?php
$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

1 回答 1

2

使用这个功能:

uasort($yourArray, function($a, $b) {
    return strcmp($a['man'], $b['man']);
});

或者看看array_multisort

于 2012-07-21T13:44:20.390 回答