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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 12:01:28 2024 UTC