array_multisort(): Array sizes are inconsistent [duplicate]
Asked Answered
E

3

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 ) 
)
Ethel answered 7/3, 2014 at 10:42 Comment(2)
Check how work array_multisort. The third example is helpful for you: array_multisort() requires an array of columnsBosnia
Just a comment on this. I was having the same issue usign PHP 5.x, when changing to PHP 7.x solved the issue.Keil
N
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..

Novanovaculite answered 8/3, 2014 at 8:4 Comment(0)
F
2

To be able to use array_multisort you should reorganize your array. See the example #3 here: https://www.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);
Foresheet answered 7/3, 2014 at 10:56 Comment(0)
I
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);
Interesting answered 20/10, 2022 at 9:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.