ID: 39456
Updated by: [EMAIL PROTECTED]
Reported By: ricardo dot matters at mindbench dot nl
-Status: Open
+Status: Bogus
Bug Type: Arrays related
Operating System: debian cur 2.6.18-1-686-bigmem
PHP Version: 5.2.0
New Comment:
Basic support issue -- should really be on the php-general list.
Nonetheless: You're not *doing* anything with the return value of
multisort() - try
$ar = multisort();
instead of
multisort($ar);
Previous Comments:
------------------------------------------------------------------------
[2006-11-10 10:08:12] ricardo dot matters at mindbench dot nl
OK, I distilled some of the code, and found another curious aspect. If
I run the code directly, the result is as expected.
But if I put the same code in a function, it does not. Seems like a
reference problem.
Working:
$weights = array(78,78,98,10,56);
$tss = array( 1,2,3,4,5);
$ar = array(
array('weight' => 78, 'ts' => 1),
array('weight' => 78, 'ts' => 2),
array('weight' => 98, 'ts' => 3),
array('weight' => 10, 'ts' => 4),
array('weight' => 56, 'ts' => 5)
);
array_multisort($weights, SORT_DESC, SORT_NUMERIC, $tss, SORT_DESC,
SORT_NUMERIC, $ar);
print_r($ar);
NOT working:
function multisort() {
$weights = array(78,78,98,10,56);
$tss = array( 1,2,3,4,5);
$ar = array(
array('weight' => 78, 'ts' => 1),
array('weight' => 78, 'ts' => 2),
array('weight' => 98, 'ts' => 3),
array('weight' => 10, 'ts' => 4),
array('weight' => 56, 'ts' => 5)
);
array_multisort($weights, SORT_DESC, SORT_NUMERIC, $tss, SORT_DESC,
SORT_NUMERIC, $ar);
return $ar;
}
multisort($ar);
print_r($ar);
------------------------------------------------------------------------
[2006-11-10 09:50:50] [EMAIL PROTECTED]
Please provide less complicated example.
If there is a problem with array_multisort(), I believe you should be
able to reproduce it with direct call.
------------------------------------------------------------------------
[2006-11-10 09:36:48] ricardo dot matters at mindbench dot nl
Description:
------------
To sort an multidimensional array, I'm using a wrapper for
array_multisort. Was working just fine, but as from 5.2, nothing is
being sorted. Documentation lacks any information if array_multisort
has been changed.
Reproduce code:
---------------
function multisort() {
$n = func_num_args();
$ar = func_get_arg($n-1);
if (!is_array($ar))
return false;
for ($i = 0; $i < $n-1; $i++) {
$col[$i] = func_get_arg($i);
}
foreach($ar as $key => $val) {
foreach($col as $kkey => $vval) {
if (is_string($vval)) {
${"subar$kkey"}[$key] = $val[$vval];
}
}
}
foreach($col as $key => $val) {
$arv[] = (is_string($val) ? ${"subar$key"} : $val);
}
$arv[] = $ar;
call_user_func_array("array_multisort", $arv);
return $ar;
}
$ar = array(
array('id' => 1, 'weight' => 78, 'ts' => 123456),
array('id' => 1, 'weight' => 78, 'ts' => 123457),
array('id' => 1, 'weight' => 98, 'ts' => 134526),
array('id' => 1, 'weight' => 10, 'ts' => 112456),
array('id' => 1, 'weight' => 56, 'ts' => 177776)
);
print_r(multisort('weight', SORT_DESC, SORT_NUMERIC, 'ts', SORT_DESC,
SORT_NUMERIC, $ar));
Expected result:
----------------
Array
(
[0] => Array
(
[id] => 1
[weight] => 98
[ts] => 134526
)
[1] => Array
(
[id] => 1
[weight] => 78
[ts] => 123457
)
[2] => Array
(
[id] => 1
[weight] => 78
[ts] => 123456
)
[3] => Array
(
[id] => 1
[weight] => 56
[ts] => 177776
)
[4] => Array
(
[id] => 1
[weight] => 10
[ts] => 112456
)
)
Actual result:
--------------
Array
(
[0] => Array
(
[id] => 1
[weight] => 78
[ts] => 123456
)
[1] => Array
(
[id] => 1
[weight] => 78
[ts] => 123457
)
[2] => Array
(
[id] => 1
[weight] => 98
[ts] => 134526
)
[3] => Array
(
[id] => 1
[weight] => 10
[ts] => 112456
)
[4] => Array
(
[id] => 1
[weight] => 56
[ts] => 177776
)
)
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=39456&edit=1