1

I am a bit curious as to why the following throws a warning.

$obj->data = array(...); // note there are values stored (size 607)

var_dump(sizeof($obj->data)); // 607

array_multisort(
    array_column($obj->data , 'col1'), SORT_ASC,
    array_column($obj->data , 'col2'), SORT_ASC,
    array_column($obj->data , 'col3'), SORT_ASC, 
$obj->data 
);

var_dump(sizeof($obj->data)); // 607

The warning I receive states

Warning: array_multisort(): Array sizes are inconsistent

Can someone give me a bit more information than the warning?

3
  • Are you array since the same ? Commented Sep 2, 2020 at 20:57
  • @Nicolas they should be. The sizes before and after (with warning) are both 607 Commented Sep 2, 2020 at 21:00
  • @Newb4YouBB counting the main array doesn't mean each array it contains have the same number of element, nor always have the column your are extracting. Meaning the count you are doing here is not relevant in your debug, the problem is in the results of array_column() calls. Commented Sep 3, 2020 at 6:50

1 Answer 1

4

You have different sizes among the arrays you pass as variables to the function:

array_multisort(
    array_column($obj->data , 'col1'), SORT_ASC,
    array_column($obj->data , 'col2'), SORT_ASC,
    array_column($obj->data , 'col3'), SORT_ASC, 
    $obj->data 
);

Probably array_column($obj->data , 'col1') (or col2 or col3) has less elements than $obj->data

For example:

$ar = array(
       array(1,2),
       array(2,1)
);
array_multisort($ar[0], $ar[1]);

Doesn't throw this warning, but

$ar = array(
       array(1,2,3),
       array(2,1)
);
array_multisort($ar[0], $ar[1]);

Will do.

Sign up to request clarification or add additional context in comments.

2 Comments

Im going to accept this because it helped me take a closer look at my actual columns. I mistakenly titled col1 when it should have been col_1. Thank you
most concise explanation of this error from all the search results returned on Stack Overflow

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.