7

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
  • 1,078
  • 5
  • 17
  • 36
  • 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

3 Answers3

23

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
  • 621
  • 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,595
  • 7
  • 43
  • 56
0

Becide function misuse, which should look like:

$keys = array_column($mar, 'wek');
array_multisort($keys, SORT_ASC, $mar);

sometimes, the reason of exception

array_multisort(): Array sizes are inconsistent

is that array(row) does not have the key by which we are sorting. It is easy possible since we have associated array with many elements, sometimes provided by 3rd parties.

The solution is to check the data before to use:

array_walk($mar, function (&$row) {
    $row['wek'] = $row['wek'] ?? [];
}); 

Full sample:

<?php

$mar = [
    [ 'dat' => 1],
    [ 'dat' => 2,  'wek'=> 9 ],
    [ 'dat' => 5 , 'wek'=> 6 ],
    [ 'dat' => 20, 'wek'=> 21 ],
    [ 'dat' => 8,  'wek'=> 14 ],
];    

print_r($mar); echo '<br>';

array_walk($mar, function (&$row) {
    $row['wek'] = $row['wek'] ?? null;
}); 
$keys = array_column($mar, 'wek');
array_multisort($keys, SORT_ASC, $mar);

print_r($mar);
Eugene Kaurov
  • 2,356
  • 28
  • 39