简体   繁体   English

按特定键对多维数组进行递归排序

[英]Sort multidimensional array recursive by specific key

I'm trying to sort this array recursively by its label: 我试图按其标签递归排序此数组:

Array
(
    [0] => Array
        (
            [id] => 6
            [label] => Bontakt
            [children] => Array
                (
                )

        )

    [1] => Array
        (
            [id] => 7
            [label] => Ampressum
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 5
                            [children] => Array
                                (
                                )

                            [label] => Bome
                        )

                    [1] => Array
                        (
                            [id] => 8
                            [children] => Array
                                (
                                )

                            [label] => Aome
                        )

                    [2] => Array
                        (
                            [id] => 10
                            [children] => Array
                                (
                                )

                            [label] => Come
                        )

                )

        )

    [2] => Array
        (
            [id] => 9
            [label] => Contakt
            [children] => Array
                (
                )

        )

    [3] => Array
        (
            [id] => 11
            [label] => Dead
            [children] => Array
                (
                )

        )

)

I've read several Questions, and I feel to be pretty close, but I can't figure out what's not working: 我读过几个问题,感觉还很接近,但是我无法弄清楚什么是行不通的:

function sortByAlpha($a, $b)
{
    return strcmp(strtolower($a['label']), strtolower($b['label'])) > 0;
}

function alphaSort(&$a)
{
    foreach ($a as $oneJsonSite)
    {
        if (count($oneJsonSite["children"]) > 0) alphaSort($oneJsonSite["children"]);
    }

    usort($a, 'sortByAlpha');
}


alphaSort($jsonSites);

Current output is like this: 当前输出是这样的:

Ampressum
    Bome
    Aome
    Come
Bontakt
Contakt
Dead

The children elements are not sorted... 子元素未排序...

Check this out: 看一下这个:

In order to be able to directly modify array elements within the loop precede $value with &. 为了能够直接在循环内修改数组元素,在$ value之前加&。 In that case the value will be assigned by reference. 在这种情况下,该值将通过引用分配。 (Picked from here: http://php.net/manual/en/control-structures.foreach.php ) (从此处选择: http : //php.net/manual/en/control-structures.foreach.php

You should try it with this one: 您应该尝试以下一种方法:

function alphaSort(&$a)
{
    foreach ($a as &$oneJsonSite)
    {
        if (count($oneJsonSite["children"]) > 0) alphaSort($oneJsonSite["children"]);
    }

    usort($a, 'sortByAlpha');
}

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

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