php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #48423 ksort applied with array_walk not working consistently
Submitted: 2009-05-29 02:33 UTC Modified: 2009-05-31 15:47 UTC
Votes:3
Avg. Score:4.0 ± 0.8
Reproduced:3 of 3 (100.0%)
Same Version:3 (100.0%)
Same OS:3 (100.0%)
From: brad at njoe dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.2.9 OS: *
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: brad at njoe dot com
New email:
PHP Version: OS:

 

 [2009-05-29 02:33 UTC] 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
        )

)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-05-31 15:47 UTC] jani@php.net
RTFM on ksort() and what it's paremeters are..
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 03 15:01:34 2025 UTC