phparrayssortingrecursionmultidimensional-array

Recursive key sorting function is not affecting lower level items


I've a simple multi array as described below that I need to order by its key label (not by key value).

array(
    1 => array(
        2 => array();
        11 => array();
        20 => array();
        31 => array();
        4 => array();
        43 => array();
        12 => array();
        3 => array();
    );
    2 => array();
    11 => array();
    20 => array();
    31 => array();
    4 => array();
    43 => array();
    12 => array();
    3 => array(); );

This is my ordering function:

private function orderByKey(&$array) {
        ksort($array);
        foreach($array as $value) {
            if (is_array($value)) {
                $this->orderByKey($value);
            }
        }
    }

The recursed items are not getting sorted.

What might be wrong here?


Solution

  • You need to access $value as a reference to the entry in $array

    private function orderByKey(&$array) {
            ksort($array);
            foreach($array as &$value) {
                if (is_array($value)) {
                    $this->orderByKey($value);
                }
            }
        }