4

I am trying to sort an array on the base of its child array using array_multisort() function......

While trying;

print_r($mar); echo '<br>';
$arr2 = array_multisort($mar, array('wek'=>SORT_ASC));
print_r($arr2);

getting error array_multisort(): Array sizes are inconsistent

the output before sorting is

Array ( 
    [0] => Array ( [dat] => 1 [wek] => 5 [mac] => A100 [mcr] => #00c8ff ) 
    [1] => Array ( [dat] => 2 [wek] => 9 [mac] => A100 [mcr] => #00c8ff ) 
    [2] => Array ( [dat] => 5 [wek] => 13 [mac] => A100 [mcr] => #00c8ff ) 
    [3] => Array ( [dat] => 5 [wek] => 6 [mac] => A101 [mcr] => #ff8800 ) 
    [4] => Array ( [dat] => 13 [wek] => 17 [mac] => A100 [mcr] => #00c8ff ) 
    [5] => Array ( [dat] => 20 [wek] => 21 [mac] => A100 [mcr] => #00c8ff ) 
    [6] => Array ( [dat] => 8 [wek] => 14 [mac] => A101 [mcr] => #ff8800 ) 
)

What i need is:

Array ( 
    [0] => Array ( [dat] => 1 [wek] => 5 [mac] => A100 [mcr] => #00c8ff ) 
    [3] => Array ( [dat] => 5 [wek] => 6 [mac] => A101 [mcr] => #ff8800 ) 
    [1] => Array ( [dat] => 2 [wek] => 9 [mac] => A100 [mcr] => #00c8ff ) 
    [2] => Array ( [dat] => 5 [wek] => 13 [mac] => A100 [mcr] => #00c8ff ) 
    [6] => Array ( [dat] => 8 [wek] => 14 [mac] => A101 [mcr] => #ff8800 ) 
    [4] => Array ( [dat] => 13 [wek] => 17 [mac] => A100 [mcr] => #00c8ff ) 
    [5] => Array ( [dat] => 20 [wek] => 21 [mac] => A100 [mcr] => #00c8ff ) 
)
VishnuPrasad
  • 998
  • 5
  • 16
  • 33
  • Check how work [array_multisort](http://www.php.net/manual/fr/function.array-multisort.php). The third example is helpful for you: **array_multisort() requires an array of columns** – Debflav Mar 07 '14 at 10:56
  • 1
    Just a comment on this. I was having the same issue usign PHP 5.x, when changing to PHP 7.x solved the issue. – celsomtrindade Feb 17 '18 at 11:03

2 Answers2

21

there is an error in below line:

$arr2 = array_multisort($mar, array('wek'=>SORT_ASC));

you are trying to store the return result to an array, but array_multisort returns boolean values not the sorted array:

do this for sorting your multidimensional array $mar:

foreach ($mar as $key => $row)
{
    $wek[$key]  = $row['wek'];
}    

// Sort the data with wek ascending order, add $mar as the last parameter, to sort by the common key

array_multisort($wek, SORT_ASC, $mar);

The $mar array is now sorted after the above operations..

Keerthi Menon
  • 601
  • 4
  • 8
2

To be able to use array_multisort you should reorganize your array. See the example #3 here: http://uk1.php.net/array_multisort

Or you can use usort, but it will renumber the keys:

<?php
$mar =Array (.
    0 => Array ( 'dat' => 1, 'wek' => 5, 'mac' => 'A100', 'mcr' => '#00c8ff' ) ,
    1 => Array ( 'dat' => 2, 'wek' => 9, 'mac' => 'A100', 'mcr' => '#00c8ff' ) ,
    2 => Array ( 'dat' => 5, 'wek' => 13, 'mac' => 'A100', 'mcr' => '#00c8ff' ) ,
    3 => Array ( 'dat' => 5, 'wek' => 6, 'mac' => 'A101', 'mcr' => '#ff8800' ) ,
    4 => Array ( 'dat' => 13, 'wek' => 17, 'mac' => 'A100', 'mcr' => '#00c8ff' ),
    5 => Array ( 'dat' => 20, 'wek' => 21, 'mac' => 'A100', 'mcr' => '#00c8ff' ) ,
    6 => Array ( 'dat' => 8, 'wek' => 14, 'mac' => 'A101', 'mcr' => '#ff8800' ) ,
);

usort($mar, function($a,$b){return $a['wek']-$b['wek'];});
print_r($mar);
Lajos Veres
  • 13,452
  • 7
  • 42
  • 56