多维数组排序的问题
程序员文章站
2022-05-06 15:23:20
...
$array= array(
'123456'=>array('aa','bb','cc'),
'12345645'=>array('aa','cc'),
'1234564555'=>array('aa','dd','cc','bb'),
'12364555'=>array('aa','dd','cc'),
);
请问怎么按子数组的长度来排,我想要的结果是
$array= array(
'12345645'=>array('aa','cc'),
'123456'=>array('aa','bb','cc'),
'12364555'=>array('aa','dd','cc'),
'1234564555'=>array('aa','dd','cc','bb'),
);
'123456'=>array('aa','bb','cc'),
'12345645'=>array('aa','cc'),
'1234564555'=>array('aa','dd','cc','bb'),
'12364555'=>array('aa','dd','cc'),
);
请问怎么按子数组的长度来排,我想要的结果是
$array= array(
'12345645'=>array('aa','cc'),
'123456'=>array('aa','bb','cc'),
'12364555'=>array('aa','dd','cc'),
'1234564555'=>array('aa','dd','cc','bb'),
);
回复讨论(解决方案)
$array = array( '123456'=>array('aa','bb','cc'), '12345645'=>array('aa','cc'), '1234564555'=>array('aa','dd','cc','bb'), '12364555'=>array('aa','dd','cc'),);foreach($array as $i=>$v) { $r[] = count($v); $k[] = $i;}array_multisort($r, $k, $array);print_r(array_combine($k, $array));
Array( [12345645] => Array ( [0] => aa [1] => cc ) [123456] => Array ( [0] => aa [1] => bb [2] => cc ) [12364555] => Array ( [0] => aa [1] => dd [2] => cc ) [1234564555] => Array ( [0] => aa [1] => dd [2] => cc [3] => bb ))
上一篇: php开发之变量