Home >Backend Development >PHP Tutorial > php二维数组排序有关问题 求帮忙排序

php二维数组排序有关问题 求帮忙排序

WBOY
WBOYOriginal
2016-06-13 13:43:14881browse

php二维数组排序问题 求帮忙排序
现有一数组

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
$a=array(
         0=>array(3=>9),
    1=>array(2=>9),
    2=>array(6=>5),
    3=>array(1=>15),
    4=>array(2=>4),
);

需要排序变为下面这样:
PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
$b=array(
    0=>array(1=>15),
    1=>array(3=>9),
    2=>array(2=>9),
    3=>array(6=>5),
    4=>array(2=>4),
    );



wish you all the best!

------解决方案--------------------
PHP code
$a=array(
    0=>array(3=>9),
    1=>array(2=>9),
    2=>array(6=>5),
    3=>array(1=>15),
    4=>array(2=>4),
);

foreach($a as $v) {
  $r[] = current($v);
  $k[] = key($v);
}

array_multisort($r, SORT_DESC, $k, SORT_DESC, $a);

print_r($a);
<br><font color="#e78608">------解决方案--------------------</font><br>
探讨
PHP code

$a=array(
0=>array(3=>9),
1=>array(2=>9),
2=>array(6=>5),
3=>array(1=>15),
4=>array(2=>4),
);

foreach($a as $v) {
$r[] = current($v);
$k[] = key($v);
}
……

------解决方案--------------------
受教了
------解决方案--------------------
我的博客里正好有一个,相比起来1楼的就是小儿科
------解决方案--------------------
不换药
PHP code

……
usort($a, create_function('$m, $n', 'return current($m)==current($n) ? (key($m)==key($n) ? 0 : key($m)<key : current print_r> Array
        (
            [1] => 15
        )

    [1] => Array
        (
            [3] => 9
        )

    [2] => Array
        (
            [2] => 9
        )

    [3] => Array
        (
            [6] => 5
        )

    [4] => Array
        (
            [2] => 4
        )

)
/**/ <div class="clear">
                 
              
              
        
            </div></key>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn