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

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

WBOY
Release: 2016-06-13 10:07:47
Original
1051 people have browsed it

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),);
Copy after login

需要排序变为下面这样:
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),    );
Copy after login


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>
Copy after login
探讨
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> Array        (            [1] => 15        )    [1] => Array        (            [3] => 9        )    [2] => Array        (            [2] => 9        )    [3] => Array        (            [6] => 5        )    [4] => Array        (            [2] => 4        ))/**/<div class="clear">
                 
              
              
        
            </div></key>
Copy after login
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template