6

I did some tasks using the array_multisort function. During the writing of the script I did a var_dump, and got different results of $mainArray depending on the PHP version. Here is the code:

$mainArray = array(
    0 =>array(
        "key1" => array(7,4,5),
        'key2' => array('cc','aa')
    )
);

foreach($mainArray as $secondArray){
    foreach($secondArray as $array){
        array_multisort($array);
    }
}

var_dump($mainArray);

Output for 4.3.10 - 4.4.9, 5.1.1 - 5.5.7:

array(1) { 
   [0]=> array(2) { 
         ["key1"]=> array(3) { 
                    [0]=> int(7) 
                    [1]=> int(4) 
                    [2]=> int(5) } 
         ["key2"]=> array(2) { 
                    [0]=> string(2) "cc" 
                    [1]=> string(2) "aa" } 
   } 
}

But Output for 4.3.0 - 4.3.9, 5.0.0 - 5.0.5 I get sorted array:

array(1) { 
  [0]=> array(2) { 
          ["key1"]=> array(3) { 
                       [0]=> int(4) 
                       [1]=> int(5) 
                       [2]=> int(7) } 
          ["key2"]=> array(2) { 
                       [0]=> string(2) "aa" 
                       [1]=> string(2) "cc" } 
   } 
 }

I knew that array_multisort($array) would not have influence on $mainArray but:

I really do not understand why in the second variant it was sorted, and in the first one is not. And, should I always check the script in different php versions?

Here you can check the script

3
  • I would rather say this is a bug in some old version of PHP... Commented Jan 9, 2014 at 7:27
  • What is the reason for using array_multisort with a single array? Commented Jan 9, 2014 at 7:46
  • It is just a part of code with problem, in main script there are array and I merged them Commented Jan 9, 2014 at 7:48

2 Answers 2

3

Your problem seems to be the different handling in the internals of foreach in the different versions of PHP. Try the following.

<?php
$mainArray = array(
    0 =>array(
        "key1" => array(7,4,5),
        'key2' => array('cc','aa')
    )
);

foreach($mainArray as &$secondArray){
    foreach($secondArray as &$array){
        array_multisort($array);
    }
}

var_dump($mainArray);

?>

As you may notice, we've included the ampersands & to the foreach loop's values, as they are necessary in PHP5+ in foreach loops to address that we want to reference the value so we can edit it directly. This will generate errors in older PHP versions though.

Also, clearly noted in the PHP.net docs:

In order to be able to directly modify array elements within the loop precede $value with &. In that case the value will be assigned by reference.

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

1 Comment

Edited my answer to more clearly explain the concept of referencing through foreach.
0

I think what is happening, is that you are getting (in the first version of the code) a copy of the array. That is the normal behaviour for a PHP script. Then you sort the copy, and the original array remains unchanged. If what you want is to sort the original array, you should do:

foreach($mainArray as &$secondArray){
    foreach($secondArray as &$array){
        array_multisort($array);
    }
}

Which gets the inner arrays by reference.

EDIT

If you want a version that will always work, you need to just replace elements of the original array.

<?php

$mainArray = array(
    0 =>array(
        "key1" => array(7,4,5),
        'key2' => array('cc','aa')
    )
);

foreach($mainArray as $secondIndex=>$secondArray){
    foreach($secondArray as $index=>$array){
        array_multisort($array);
        $secondArray[$index] = $array;
    }
    $mainArray[$secondIndex] = $secondArray;
}

var_dump($mainArray);

Check it on http://3v4l.org/Bk1YM

4 Comments

I have just one version of the code, but tested on different versions of PHP.
But the main problem is not how to make it work, but how this problem appeared. Because, for example, if my PHPv =5.0.0, I will have a nice result, and even do not test on other versions. Have tested all your code on all versions?I do not think so
I understand. I had a similar issue when PHP 5 was first released. All I can tell you, is you should run your tests in the new version before upgrading, there is no other safe way of doing it. But this happens with every version change of every technology.
ehh, but for example, part of my code(module) my colleges can take on another project with another PHPv and we will have a problems. Maybe there is any logical answer why this happened.

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.