Alphabetically sort multidimensional array keys and values ​​recursively in PHP

I need help sorting the keys and values ​​of the array below in alphabetical order:

$unsorted = [
    'D' => [
        'C' => ['c', 'b', 'a'],
        'B' => 'bvalue',
        'A' => ['a', 'c', 'b'],
    ],
    'C' => 'cvalue',
    'B' => 'bvalue',
    'A' => [
        'Z' => 'zvalue',
        'A' => 'avalue',
        'B' => 'bvalue',
    ]
];

      

The sort must be recursive as the array above is multidimensional. It contains other arrays (numeric indices and associative) as its values.

I was able to sort the keys of the array recursively using this function:

function sortKeysRecursive(&$array)
{
    foreach ($array as $key => &$value) {
        if (is_array($value)) {
            sortKeysRecursive($value);
        }
    }

    ksort($array);
}

      

However, I was unable to sort the values ​​without going into the already sorted keys. To sort the values, I tried to apply this function:

function sortValuesRecursive(&$array)
{
    foreach ($array as &$value) {
        if (is_array($value)) {
            sortValuesRecursive($value);
        }
    }
    asort($value);
}

sortKeysRecursive($unsorted);
sortValuesRecursive($unsorted);

      

But this is one thing or the other. Both functions applied to the same array always interfere with other functions.

I am expecting to create a sorted array that looks like this:

$sorted = [
    'A' => [
        'A' => 'avalue',
        'B' => 'bvalue',
        'Z' => 'zvalue',
    ],
    'B' => 'bvalue',
    'C' => 'cvalue',
    'D' => [
        'A' => ['a', 'b', 'c'],
        'B' => 'bvalue',
        'C' => ['a', 'b', 'c'],
    ],
];

      

I would be grateful for your help.

+3


source to share


3 answers


you need to check that the keys are numeric or alphabetic. try below, you may need to change the terms for your purpose:

<?php

function isAssoc(array $arr)
{
    return array_keys($arr) !== range(0, count($arr) - 1);
}

function sortArray(&$arr){
    if(isAssoc($arr)){
        ksort($arr);
    } else{
        asort($arr);
    }
    foreach ($arr as &$a){
        if(is_array($a)){
            sortArray($a);
        }
    }
}

$unsorted = array(
    'D' => array(
        'C' => array('c', 'b', 'a'),
        'B' => 'bvalue',
        'A' => array('a', 'c', 'b'),
    ),
    'C' => 'cvalue',
    'B' => 'bvalue',
    'A' => array(
        'Z' => 'zvalue',
        'A' => 'avalue',
        'B' => 'bvalue',
    )
);
sortArray($unsorted);

print_r($unsorted);

      



Output

Array
(
    [A] => Array
        (
            [A] => avalue
            [B] => bvalue
            [Z] => zvalue
        )

    [B] => bvalue
    [C] => cvalue
    [D] => Array
        (
            [A] => Array
                (
                    [0] => a
                    [2] => b
                    [1] => c
                )

            [B] => bvalue
            [C] => Array
                (
                    [2] => a
                    [1] => b
                    [0] => c
                )

        )

)

      

+2


source


Use the following method and loop through the array $unsorted

and conditionally sort:

function recursiveSort(array &$unsorted) {

    // Sort array keys
    ksort($unsorted);

    foreach ($unsorted as $key => $array) {
        if (!is_array($array)) {
            continue;
        }

        if (is_numeric(key($array))) {
            asort($array);
        } else {
            recursiveSort($array);
        }
        $unsorted[$key] = $array;
    }
}

      

Application:



recursiveSort($unsorted);

      

Hope it helps

0


source


function sortKeysRecursive(&$array)
{
    foreach ($array as $key => &$value) {
        if (is_array($value)) {
            sortKeysRecursive($value);
        }
    }

    ksort($array);
}

/**
 * Sort only the arrays that doesn't have a child array, this way
 * we don't mess with what sortKeysRecursive has sorted
 */
function sortValuesRecursive(&$array)
{
    $isArray = false;

    foreach ($array as $key => &$value) {
        if (is_array($value)) {
            $isArray = true;
            sortValuesRecursive($value);
        }
    }

    if ($isArray === false) {
        asort($array);
    }
}

sortKeysRecursive($unsorted);
sortValuesRecursive($unsorted);

      

0


source







All Articles