简体   繁体   English

在PHP中以递归方式按字母顺序对多维数组的键和值进行排序

[英]Alphabetically sort multidimensional array's keys and values recursively in PHP

I need help sorting keys and values of the array below alphabetically: 我需要帮助按字母顺序排列数组的键和值:

$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 has to be recursive as the array above is multidimensional. 排序必须是递归的,因为上面的数组是多维的。 It holds other arrays (numerically indexed and associative) as its values. 它将其他数组(数字索引和关联)作为其值。

I managed to sort the array's keys 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 messing with 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 it's one or the other. 但它是一个或另一个。 Both function applied to the same array always mess the other functions work. 两个函数应用于同一个数组总是混乱其他函数工作。

I'd expect to produce 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'd be grateful for help. 我很感激你的帮助。

you need to check keys are numeric or alphabetic. 你需要检查键是数字还是字母。 try below solution you may need to modify conditions 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
                )

        )

)

Use following method and loop through the $unsorted array and sort conditionally: 使用以下方法并循环遍历$unsorted数组并有条件地排序:

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;
    }
}

Usage: 用法:

recursiveSort($unsorted);

Hope it helps 希望能帮助到你

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM