给定一个二维数组,如下,请对它的键/值进行排序。注意:5,2,7对应的值里的数组不确定的,会很大eg:$arr=[5=>[1,4,8,98,3,33,17,76,63],2=>[3,12,5,88,101,32,22,77,1],7=>[5,6,90,31,22,21,88,36,102]];排序之后的数组为:$arr=[2=>[1,3,5,12,22,32,77,88,101],5=>[1,3,4,8,17,33,63,76,98],7=>[5,6,21,22,31,36,88,90,102],];请问如何实现排序算法,不允许使用php自带的函数,如sort、ksort等?
2 回答
绝地无双
TA贡献1946条经验 获得超4个赞
$arr=[5=>[1,4,8,98,3,33,17,76,63],2=>[3,12,5,88,101,32,22,77,1],7=>[5,6,90,31,22,21,88,36,102]];ksort($arr);$result=array_map(function($item){sort($item);return$item;},$arr);var_dump($result);结果array(3){[2]=>array(9){[0]=>int(1)[1]=>int(3)[2]=>int(5)[3]=>int(12)[4]=>int(22)[5]=>int(32)[6]=>int(77)[7]=>int(88)[8]=>int(101)}[5]=>array(9){[0]=>int(1)[1]=>int(3)[2]=>int(4)[3]=>int(8)[4]=>int(17)[5]=>int(33)[6]=>int(63)[7]=>int(76)[8]=>int(98)}[7]=>array(9){[0]=>int(5)[1]=>int(6)[2]=>int(21)[3]=>int(22)[4]=>int(31)[5]=>int(36)[6]=>int(88)[7]=>int(90)[8]=>int(102)}}
添加回答
举报
0/150
提交
取消