排序多维数组

时间:2012-07-19 10:13:33

标签: php recursion return

  

可能重复:
  How do I sort a multidimensional array in php

我有多维数组,用于解释:

$ 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));

2 个答案:

答案 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));