php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #52177 please implement recursive ksort
Submitted: 2010-06-24 23:19 UTC Modified: 2021-12-01 15:56 UTC
Votes:3
Avg. Score:4.7 ± 0.5
Reproduced:3 of 3 (100.0%)
Same Version:1 (33.3%)
Same OS:1 (33.3%)
From: dsrthorne at gmail dot com Assigned: cmb (profile)
Status: Wont fix Package: Arrays related
PHP Version: Irrelevant OS: linux
Private report: No CVE-ID: None
 [2010-06-24 23:19 UTC] dsrthorne at gmail dot com
Description:
------------
I've a problem wherein I'm using SimpleTest's testIdentical to compare nested arrays, and if the key order differs it throws an error.  What I really care about is making sure the same content exists in both, order independent.  My solution was to implement a recursive ksort to run the nested arrays through to guarantee key order was the same, regardless of how the arrays are given to me initially.  It seems like it would be useful to have this functionality in base php.

Test script:
---------------
My short php-based implementation looks like this:

function rksort($array = null) {
        if (!@is_array($array))
                return $array;

        ksort($array);

        foreach ($array as $index => $notused) {
                $array[$index] = rksort($array[$index]);
        }

        return $array;
}


Expected result:
----------------
Results confirm expectations: the data structure is key sorted recursively using ksort.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-12-01 15:56 UTC] cmb@php.net
-Status: Open +Status: Wont fix -Assigned To: +Assigned To: cmb
 [2021-12-01 15:56 UTC] cmb@php.net
I'm having a hard time to understand why we should introduce a
recursive ksort(), but not at the same time recursive variants of
the other sorting functions.  Then again, that would be a dozen
new functions which are likely not too often needed.  Your
userland implementation looks fine, so where is the point in
adding that to ext/standard (besides that a C implementation
*might* be more performant, what would need to be measured,
though)?

I'm closing this as WONTFIX.  If anybody is still interested in
this, please pursue the RFC process[1].

[1] <https://wiki.php.net/rfc/howto>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 13:01:31 2024 UTC