1

I thought that the function array_multisort can sort multidimensional array, but this code does not work.

Code:

$values = array(0, 2, 1, array(0, 2, 1));
array_multisort($values, SORT_ASC);
var_dump($values);

Return:

array(4) [
   0 => 0
   1 => 1
   2 => 2
   3 => array(3) [
      0 => 0
      1 => 2 //should be 1
      2 => 1 //should be 2
   ]
]

Why the array in array is not sorted? Thanks

user1518183
  • 4,651
  • 5
  • 28
  • 39

2 Answers2

3

You can try

sort_recursive($values);
var_dump($values);

Output

array (size=4)
  0 => int 0
  1 => int 1
  2 => int 2
  3 => 
    array (size=3)
      0 => int 0
      1 => int 1
      2 => int 2

Function Used

function sort_recursive(&$array) {
    sort($array);
    foreach ( $array as &$v ) {
        is_array($v) and sort_recursive($v);
    }
}
Baba
  • 94,024
  • 28
  • 166
  • 217
0

is because the array() is in a multidimentional form

try this link to see how to sort multidimensional array click here

Community
  • 1
  • 1
Jhonathan H.
  • 2,734
  • 1
  • 19
  • 28