通过子阵列的数量对多维数组进行排序

时间:2016-04-19 14:33:52

标签: php arrays sorting

我有这样的数组:

Array
(
  [28748] => stdClass Object
    (
        [uid] => 28748
        [status] => 1
        [children] => Array
            (
                [29163] => stdClass Object
                    (
                        [uid] => 29163
                        [status] => 1
                    )

            )

    )

[28708] => stdClass Object
    (
        [uid] => 28708
        [status] => 1
        [children] => Array
            (
                [27104] => stdClass Object
                    (
                        [uid] => 27104
                        [status] => 1
                        [children] => Array
                            (
                                [28250] => stdClass Object
                                    (
                                        [uid] => 28250
                                        [status] => 1
                                    )

                            )

                    )

                [29448] => stdClass Object
                    (
                        [uid] => 29448
                        [status] => 1
                        [children] => Array
                            (
                                [28528] => stdClass Object
                                    (
                                        [uid] => 28528
                                        [status] => 1
                                    )

                                [28329] => stdClass Object
                                    (
                                        [uid] => 28329
                                        [status] => 1
                                        [children] => Array
                                            (
                                                [28533] => stdClass Object
                                                   (
                                                  [uid] => 28533
                                                  [status] => 1
                                               )
                                    )

                                [26548] => stdClass Object
                                    (
                                        [uid] => 26548
                                        [status] => 1
                                     )

                            )

                    )

            )

    )

我想通过子数组的计数对数组进行排序,因此不会出现更大的子项。维度的水平是无限的。在我的情况下,我想成为这个数组:

Array
(

[28708] => stdClass Object
    (
        [uid] => 28708
        [status] => 1
        [children] => Array
            (
                [29448] => stdClass Object
                    (
                        [uid] => 29448
                        [status] => 1
                        [children] => Array
                            (

                                [28329] => stdClass Object
                                    (
                                        [uid] => 28329
                                        [status] => 1
                                        [children] => Array
                                            (
                                                [28533] => stdClass Object
                                                   (
                                                  [uid] => 28533
                                                  [status] => 1
                                               )
                                    )

                                [28528] => stdClass Object
                                    (
                                        [uid] => 28528
                                        [status] => 1
                                    )

                                [26548] => stdClass Object
                                    (
                                        [uid] => 26548
                                        [status] => 1
                                     )

                            )

                    )

                    [27104] => stdClass Object
                    (
                        [uid] => 27104
                        [status] => 1
                        [children] => Array
                            (
                                [28250] => stdClass Object
                                    (
                                        [uid] => 28250
                                        [status] => 1
                                    )

                            )

                    )

            )

    )

[28748] => stdClass Object
    (
        [uid] => 28748
        [status] => 1
        [children] => Array
            (
                [29163] => stdClass Object
                    (
                        [uid] => 29163
                        [status] => 1
                    )

            )

    )

这是我正在检查的新数组:

Array
(
 [27104] => stdClass Object
    (
        [uid] => 27104
        [status] => 1
        [children] => Array
            (
            [28250] => stdClass Object
                    (
                        [uid] => 28250
                        [status] => 1
                        [children] => Array
                            (
                                [28839] => stdClass Object
                                    (
                                        [uid] => 28839
                                        [status] => 1
                                        [children] => Array
                                            (
                                                [27102] => stdClass Object
                                                    (
                                                        [uid] => 27102
                                                        [status] => 1
                                                    )
                                            )
                                    )
                            )
                    )

                [26551] => stdClass Object
                    (
                        [uid] => 26551
                        [status] => 1
                        [children] => Array
                            (
                                [25368] => stdClass Object
                                    (
                                        [uid] => 25368
                                        [status] => 1
                                    )                            
                            )
                    )
            )
    )

[28708] => stdClass Object
    (
        [uid] => 28708
        [status] => 1
        [children] => Array
            (
                [29448] => stdClass Object
                    (
                        [uid] => 29448
                        [status] => 1
                        [children] => Array
                            (
                                [28528] => stdClass Object
                                    (
                                        [uid] => 28528
                                        [status] => 1
                                    )

                                [28329] => stdClass Object
                                    (
                                        [uid] => 28329
                                        [status] => 1
                                        [children] => Array
                                            (
                                                [28654] => stdClass Object
                                                    (
                                                        [uid] => 28654
                                                        [status] => 1
                                                    )
                                            )
                                    )

                                [26548] => stdClass Object
                                    (
                                        [uid] => 26548
                                        [status] => 1
                                    )
                            )
                    )
            )
    )

[28748] => stdClass Object
    (
        [uid] => 28748
        [status] => 1
        [children] => Array
            (
                [28838] => stdClass Object
                    (
                        [uid] => 28838
                        [status] => 1 
                    )

                [28685] => stdClass Object
                    (
                        [uid] => 28685
                        [status] => 1
                    )

                [29163] => stdClass Object
                    (
                        [uid] => 29163
                        [status] => 1
                    )
            )
    )
  )

使用“sortByNumChildren($ data)后”我正在

  Array
 (
  [28748] => stdClass Object
    (
        [uid] => 28748
        [status] => 1
        [children] => Array
            (
                [29163] => stdClass Object
                    (
                        [uid] => 29163
                        [status] => 1
                    )

                [28685] => stdClass Object
                    (
                        [uid] => 28685
                        [status] => 1
                    )

                [28838] => stdClass Object
                    (
                        [uid] => 28838
                        [status] => 1
                    )
            )
    )

[28708] => stdClass Object
    (
        [uid] => 28708
        [status] => 1
        [children] => Array
            (
                [29448] => stdClass Object
                    (
                        [uid] => 29448
                        [status] => 1
                        [children] => Array
                            (
                                [26548] => stdClass Object
                                    (
                                        [uid] => 26548
                                        [status] => 1
                                    )

                                [28329] => stdClass Object
                                    (
                                        [uid] => 28329
                                        [status] => 1
                                        [children] => Array
                                            (
                                                [28654] => stdClass Object
                                                    (
                                                        [uid] => 28654
                                                        [status] => 1
                                                    )
                                            )
                                    )
                                [28528] => stdClass Object
                                    (
                                        [uid] => 28528
                                        [status] => 1
                                    )
                           )
                    )
            )
    )

[27104] => stdClass Object
    (
        [uid] => 27104
        [status] => 1
        [children] => Array
            (
                [26551] => stdClass Object
                    (
                        [uid] => 26551
                        [status] => 1
                        [children] => Array
                            (
                                [25368] => stdClass Object
                                    (
                                        [uid] => 25368
                                        [status] => 1
                                    )
                            )
                    )

                [28250] => stdClass Object
                    (
                        [uid] => 28250
                        [status] => 1
                        [children] => Array
                            (
                                [28839] => stdClass Object
                                    (
                                        [uid] => 28839
                                        [status] => 1
                                        [children] => Array
                                            (
                                                [27102] => stdClass Object
                                                    (
                                                        [uid] => 27102
                                                        [status] => 1
                                                    )

                                            )
                                    )
                           )
                    )
            )
    )

这看起来不正确,因为它没有按所有孩子的计数排序。 排序应该根据所有后代的数量。

请检查一下。

2 个答案:

答案 0 :(得分:0)

您可以使用递归函数,使用uasort对每个级别进行排序:

function sortByNumChildren(&$array) {
    foreach ($array as $key => &$obj) {
        if (isset($obj->children)) sortByNumChildren($obj->children);
    }
    uasort($array, function ($a, $b) { 
        if (!isset($a->children)) return 1;
        if (!isset($b->children)) return -1;
        return count($b->children) - count($a->children);
    });
}

按如下方式调用此函数:

sortByNumChildren($data);

eval.in

上查看它与输出一起运行

此代码将按(直系)子项的数量排序。请参阅下文,了解后代数量。

所有后代

的数量排序的代码

这里要求的是另一段代码,它按照子孙的数量对嵌套数组进行排序,所以还要计算孙子孙女及其子女等等:

function sortByNumChildren(&$array) {
    $childCounts = array();
    foreach ($array as &$obj) {
        $childCounts[$obj->uid] = isset($obj->children) ? 
                sortByNumChildren($obj->children) : 0;
    }
    uasort($array, function ($a, $b) use ($childCounts) { 
        return $childCounts[$b->uid] - $childCounts[$a->uid];
    });
    return count($array) + array_sum($childCounts);
}

按如下方式调用此函数:

sortByNumChildren($data);

eval.in

上查看它与输出一起运行

答案 1 :(得分:0)

非常感谢你的答案@trincot。

但我认为我错过了一件事 如果孩子的总数量更大,那么该数组将首先出现。

正如我的数组中所提到的,假设我在第一个子数组中又增加了2个元素

28748 => stdClass Object

(
    [uid] => 28748
    [status] => 1
    [children] => Array
        (
            [29163] => stdClass Object
                (
                    [uid] => 29163
                    [status] => 1
                ) 
              [29173] => stdClass Object
                (
                    [uid] => 29173
                    [status] => 1
                )
               [29174] => stdClass Object
                (
                    [uid] => 29174
                    [status] => 1
                )

        )

)

第一个子阵列中的儿童总数= 3

第二个子阵列中的儿童总数= 7

但是第二个子数组中的子项总数(计算内部子项)也更大,因此第二个子数组将首先出现,并且此shold与内部部分相同。

请研究一下。