我有多维数组,用于解释:
$ mas = array(“s”,array(“c”,“b”,“a”),array(3,5,1),“a”,array(“s”,array) ( “Z”, “A”)));
我想排序(升序)所有数组元素,我写递归函数,但它不起作用,请告诉我,我有谁错了?
$mas = array("s",array("c","b","a"),array(3,5,1),"a",array("s",array("z","a")));
function sortsort($m) {
sort($m);
foreach ($m as &$v) {
if (is_array($v)) {
sortsort($v);
}
}
return $m;
}
var_dump(sortsort($mas));
答案 0 :(得分:1)
使用
function aasort (&$array, $key) {
$sorter=array();
$ret=array();
reset($array);
foreach ($array as $ii => $va) {
$sorter[$ii]=$va[$key];
}
asort($sorter);
foreach ($sorter as $ii => $va) {
$ret[$ii]=$array[$ii];
}
$array=$ret;
}
aasort($ your_array, “命令”);
答案 1 :(得分:1)
试试这个
$mas = array("s",array("c","b","a"),array(3,5,1),"a",array("s",array("z","a")));
function sortsort(&$m) {
sort($m);
foreach ($m as $k=>$v) {
if (is_array($v)) {
sortsort($m[$k]);
}
}
return $m;
}
print_r(sortsort($mas));