php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #49069 regression in array_multisort()
Submitted: 2009-07-27 03:52 UTC Modified: 2009-07-27 11:54 UTC
From: weierophinney@php.net Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.3.0 OS: Ubuntu 9.04
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: weierophinney@php.net
New email:
PHP Version: OS:

 

 [2009-07-27 03:52 UTC] weierophinney@php.net
Description:
------------
Prior to 5.3.0, array_multisort() expected no references; the last array provided would be assumed to be a reference, and updated by the function during processing.

Starting in 5.3.0, array_multisort() now expects all arguments to be passed by reference, raising a warning if it detects any are passed by copy. This also includes the various sort flags (which simply does not make sense, since the SORT_* constants are typically passed directly).

In the reproduce code below, if you pass all arguments by reference, you get the expected result. However, this is a BC break from previous versions of PHP, as you were not required to pass by reference.

An additional BC break occurs with the results: if you ignore the warning, the results returned differ from what was returned prior to 5.3 (this is shown below). You will only get the expected results if, again, all arguments are passed by reference.

Reproduce code:
---------------
$array1 = array('foo', 'bar', 'baz');
$array2 = array('baz', 'bar', 'foo');
$array3 = array('Foo', 'Bar', 'Baz');
$sort1a  = SORT_ASC;
$sort1b  = SORT_STRING;
$sort2a  = SORT_ASC;
$sort2b  = SORT_REGULAR;

$args = array($array1, $sort1a, $sort1b, $array2, $sort2a, $sort2b, &$array3);
call_user_func_array('array_multisort', $args);
var_export($array3);


Expected result:
----------------
array (
  0 => 'Bar',
  1 => 'Baz',
  2 => 'Foo',
)

Actual result:
--------------
Warning: Parameter 1 to array_multisort() expected to be a reference, value given in /home/matthew/tmp/test.php on line 23
array (
  0 => 'Foo',
  1 => 'Bar',
  2 => 'Baz',
)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-07-27 04:25 UTC] rasmus@php.net
Note though that you only see this when you call it via call_user_func().  Change your script to call it directly:

array_multisort($array1, $sort1a, $sort1b, $array2, $sort2a, $sort2b, &$array3);

and you get your expected output and no warning.  This is more about call_user_func() than about array_multisort() specifically.
 [2009-07-27 08:53 UTC] jani@php.net
And this isn't even a bug but just how it's supposed to work. See also:
bug #43568 (and read comments by uw/johannes :)
 [2009-07-27 11:54 UTC] weierophinney@php.net
The explanation makes sense. We are using call_user_func_array() as the number of arguments is unknown at first, and can vary between invocations. Based on reading bug 43568, I can see that the previous behavior was incorrect -- and now that we've corrected the issue once, we're unlikely to run into it again.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 23:01:29 2024 UTC