ID: 48423
Updated by: [email protected]
Reported By: brad at njoe dot com
-Status: Open
+Status: Bogus
Bug Type: Arrays related
Operating System: *
PHP Version: 5.2.9
New Comment:
RTFM on ksort() and what it's paremeters are..
Previous Comments:
------------------------------------------------------------------------
[2009-05-29 02:33:31] brad at njoe dot com
Description:
------------
While using array_walk() to apply ksort() to a multidimensional array,
I experienced some unusual results. I expected that since each
"sub-array" contained identical keys and values that they would all be
sorted in the same manner. See the example code/expected output below
for more information.
I've tried this on Windows (ver. 5.2.9-2) as well as a Linux box (ver.
5.2.6) and both yielded the same results. If I manually "walk" ksort()
through the array, i.e.:
ksort($test[0]);
ksort($test[1]);
ksort($test[2]);
ksort($test[3]);
ksort($test[4]);
ksort($test[5]);
then the results are as expected. I've tried with varying number of
array elements in the parent array as well as changing the keys around;
no matter what I did, the array in $test[1] was always sorted
incorrectly (and unpredictably at that).
Reproduce code:
---------------
<?php
$test = array(
array('key' => 'value1', 'this' => 'value2', 'word' => 'value3',
'blah' => 'value4', 'foo' => 'value5', 'bar' => 'value6'),
array('key' => 'value1', 'this' => 'value2', 'word' => 'value3',
'blah' => 'value4', 'foo' => 'value5', 'bar' => 'value6'),
array('key' => 'value1', 'this' => 'value2', 'word' => 'value3',
'blah' => 'value4', 'foo' => 'value5', 'bar' => 'value6'),
array('key' => 'value1', 'this' => 'value2', 'word' => 'value3',
'blah' => 'value4', 'foo' => 'value5', 'bar' => 'value6'),
array('key' => 'value1', 'this' => 'value2', 'word' => 'value3',
'blah' => 'value4', 'foo' => 'value5', 'bar' => 'value6'),
array('key' => 'value1', 'this' => 'value2', 'word' => 'value3',
'blah' => 'value4', 'foo' => 'value5', 'bar' => 'value6')
);
array_walk($test, 'ksort');
print_r($test);
?>
Expected result:
----------------
Array
(
[0] => Array
(
[bar] => value6
[blah] => value4
[foo] => value5
[key] => value1
[this] => value2
[word] => value3
)
[1] => Array
(
[bar] => value6
[blah] => value4
[foo] => value5
[key] => value1
[this] => value2
[word] => value3
)
[2] => Array
(
[bar] => value6
[blah] => value4
[foo] => value5
[key] => value1
[this] => value2
[word] => value3
)
[3] => Array
(
[bar] => value6
[blah] => value4
[foo] => value5
[key] => value1
[this] => value2
[word] => value3
)
[4] => Array
(
[bar] => value6
[blah] => value4
[foo] => value5
[key] => value1
[this] => value2
[word] => value3
)
[5] => Array
(
[bar] => value6
[blah] => value4
[foo] => value5
[key] => value1
[this] => value2
[word] => value3
)
)
Actual result:
--------------
Array
(
[0] => Array
(
[bar] => value6
[blah] => value4
[foo] => value5
[key] => value1
[this] => value2
[word] => value3
)
[1] => Array
(
[foo] => value5
[bar] => value6
[blah] => value4
[word] => value3
[this] => value2
[key] => value1
)
[2] => Array
(
[bar] => value6
[blah] => value4
[foo] => value5
[key] => value1
[this] => value2
[word] => value3
)
[3] => Array
(
[bar] => value6
[blah] => value4
[foo] => value5
[key] => value1
[this] => value2
[word] => value3
)
[4] => Array
(
[bar] => value6
[blah] => value4
[foo] => value5
[key] => value1
[this] => value2
[word] => value3
)
[5] => Array
(
[bar] => value6
[blah] => value4
[foo] => value5
[key] => value1
[this] => value2
[word] => value3
)
)
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=48423&edit=1