Reputation: 57
I find other thread about sort multidimensional array, but it's not the same problem.
$arr[22][] = 45;
$arr[22][] = 44;
$arr[22][] = 99;
$arr[23][] = 95;
$arr[23][] = 55;
$arr[23][] = 1;
echo "<pre>";
print_r($arr);
echo "</pre>";
I want to sort the value inside subarray, not the value between sub array.
The expected result is
[22] => Array [0 ] => 44 [1] => 45 [2] => 99
[23] => Array [0 ] => 1 [1] => 55 [2] => 95
I try with
array_multisort($arr[22], SORT_ASC, SORT_NUMERIC
, $arr[23], SORT_ASC, SORT_NUMERIC);
but it's not correct anyway.
How could I do?
Upvotes: 2
Views: 830
Reputation: 1
// if arr is a multidimensional array
foreach($arr as &$v)
sort($v[0]); // sort the subarray
unset($v);
if you plan on using $v in another foreach later on any !isset($v[0]) may infect the next loop, wierd
Upvotes: 0
Reputation: 4288
Simple enough:
foreach($arr as &$v) {
sort($v);
}
The &$v
allow the values to be iterated over by reference, allowing modification inside the foreach loop (and thus sorting of each subarray).
Upvotes: 2