在PHP中递归地对多维数组的键和值按字母顺序排序 [英] Alphabetically sort multidimensional array's keys and values recursively in PHP

查看:115
本文介绍了在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'],
    ],
];

感谢您的帮助.

推荐答案

您需要检查键是数字还是字母.请尝试以下解决方案,您可能需要根据自己的情况修改条件:

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

输出

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
                )

        )

)

这篇关于在PHP中递归地对多维数组的键和值按字母顺序排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆