php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #49353 call_user_array_func()/array_multisort()
Submitted: 2009-08-25 01:29 UTC Modified: 2009-08-25 14:54 UTC
Votes:3
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:1 (50.0%)
From: ws at develtheory dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.3.0 OS: Cent5
Private report: No CVE-ID: None
 [2009-08-25 01:29 UTC] ws at develtheory dot com
Description:
------------
Warning: Parameter 2 to array_multisort() expected to be a reference, 
value given

I realize tickets similar to this (#49069, #49241) have already been 
marked as bogus, but I'm not finding a good solution or explanation of 
call_user_func_array's breakage of this between 5.2 and 5.3. 

The documentation of call_user_func_array describes it as such: "Note: 
Referenced variables in param_arr are passed to the function by a 
reference, others are passed by a value. In 
other words, it does not depend on the function signature whether the 
parameter is passed by a value or by a 
reference."

This in mind, I'm not sure why SORT_ASC or SORT_REGULAR would _ever_ 
need to be sent in as a reference. This perplexes me.

It seems to me like a properly referenced array should pass in fine. 
Had weierophinney@php.net written the $args line in OP #49069 as such:

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

it should work, right?
I have attached example code that in my eyes (and others in #php.pecl 
and #phpc) should work in 5.3 and does 
already work in 5.2.

Since weierophinney@php.net did not provide a fix in that ticket, 
could someone point me to a solution, should the functionality still 
be sans-bug?



Reproduce code:
---------------
/* first name params */
$fnames = array('9'=>'FirstOne', '2'=>'FirstTwo');
$fsort = SORT_DESC;
$fsort_type = SORT_STRING;

/* the array itself */

$the_array = 
array(
	'9' => array(
		'id'=>'9',
		'first_name'=>'FirstOne',
		'last_name'=>'LastOne'
	),
	'4' => array(
		'id'=>'4',
		'first_name'=>'FirstTwo',
		'last_name'=>'LastTwo'
	)
);

$params[] = &$fnames;
$params[] = $fsort;
$params[] = $fsort_type;

$params[] = &$the_array;

call_user_func_array('array_multisort', $params); //sends warning, doesn't sort properly
//array_multisort(&$fnames, $fsort, $fsort_type, &$the_array); //works fine

var_dump($the_array);

Expected result:
----------------
array
	0 => 
	array
		'id' => string '4' (length=1)
		'first_name' => string 'FirstTwo' (length=8)
		'last_name' => string 'LastTwo' (length=7)
	1 => 
	array
		'id' => string '9' (length=1)
		'first_name' => string 'FirstOne' (length=8)
		'last_name' => string 'LastOne' (length=7)


Actual result:
--------------
Parameter 2 to array_multisort() expected to be a reference, value given

array
	0 => 
	array
		'id' => string '9' (length=1)
		'first_name' => string 'FirstOne' (length=8)
		'last_name' => string 'LastOne' (length=7)
	1 => 
	array
		'id' => string '4' (length=1)
		'first_name' => string 'FirstTwo' (length=8)
		'last_name' => string 'LastTwo' (length=7)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-08-25 06:42 UTC] jani@php.net
It's not broken, it was fixed. Please read again the explanation by Scott in bug #49241 (it worked by accident in 5.2..)
 [2009-08-25 14:54 UTC] ws at develtheory dot com
Note to all:

The solution to this *COUGH* bug is to send all params in as a 
reference, even though array_multisort clearly shouldn't require 
reference for any SORT_ args, and values/refs should be respected by 
call_user_func_array.

Realistically, it seems that call_user_func_array is still a very 
nasty function.

The hack-ish diff to make the example code work:

-$params[] = $fsort;
+$params[] = &$fsort;
-$params[] = $fsort_type;
+$params[] = &$fsort_type;
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Mon Oct 18 18:03:40 2021 UTC