From:             brad at njoe dot com
Operating system: Windows & *nix
PHP version:      5.2.9
PHP Bug Type:     Arrays related
Bug description:  ksort applied with array_walk not working consistently

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 bug report at http://bugs.php.net/?id=48423&edit=1
-- 
Try a CVS snapshot (PHP 5.2):        
http://bugs.php.net/fix.php?id=48423&r=trysnapshot52
Try a CVS snapshot (PHP 5.3):        
http://bugs.php.net/fix.php?id=48423&r=trysnapshot53
Try a CVS snapshot (PHP 6.0):        
http://bugs.php.net/fix.php?id=48423&r=trysnapshot60
Fixed in CVS:                        
http://bugs.php.net/fix.php?id=48423&r=fixedcvs
Fixed in CVS and need be documented: 
http://bugs.php.net/fix.php?id=48423&r=needdocs
Fixed in release:                    
http://bugs.php.net/fix.php?id=48423&r=alreadyfixed
Need backtrace:                      
http://bugs.php.net/fix.php?id=48423&r=needtrace
Need Reproduce Script:               
http://bugs.php.net/fix.php?id=48423&r=needscript
Try newer version:                   
http://bugs.php.net/fix.php?id=48423&r=oldversion
Not developer issue:                 
http://bugs.php.net/fix.php?id=48423&r=support
Expected behavior:                   
http://bugs.php.net/fix.php?id=48423&r=notwrong
Not enough info:                     
http://bugs.php.net/fix.php?id=48423&r=notenoughinfo
Submitted twice:                     
http://bugs.php.net/fix.php?id=48423&r=submittedtwice
register_globals:                    
http://bugs.php.net/fix.php?id=48423&r=globals
PHP 4 support discontinued:          http://bugs.php.net/fix.php?id=48423&r=php4
Daylight Savings:                    http://bugs.php.net/fix.php?id=48423&r=dst
IIS Stability:                       
http://bugs.php.net/fix.php?id=48423&r=isapi
Install GNU Sed:                     
http://bugs.php.net/fix.php?id=48423&r=gnused
Floating point limitations:          
http://bugs.php.net/fix.php?id=48423&r=float
No Zend Extensions:                  
http://bugs.php.net/fix.php?id=48423&r=nozend
MySQL Configuration Error:           
http://bugs.php.net/fix.php?id=48423&r=mysqlcfg

Reply via email to