|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2009-05-31 15:47 UTC] jani@php.net
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Dec 05 15:00:01 2025 UTC |
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 ) )