PHP -递归数组根据子元素特定的键将值递增到父元素

lpwwtiir  于 12个月前  发布在  PHP
关注(0)|答案(2)|浏览(105)

我每个人!我需要帮助...
我正在努力实现一个递归函数,可以根据各自的“项目”查尔兹,在所有的父母中增加“新”键。
我有一个看起来像这样的数组:

$tree = [
  "FOLDER_1" => [
    "name" => "FOLDER 1",
    "new" => 0,
    "items" => [
      "SUB_FOLDER_1_1" => [
        "name" => "SUB FOLDER 1 1',
        "new" => 0,
        "items" => [
          "SUB_FILE_1_1_1" => [
            "name" => "SUB FILE 1 1 1',
            "new" => 0
          ],
          "SUB_FILE_1_1_2" => [
            "name" => "SUB FILE 1 1 2',
            "new" => 1
          ],
        ]
      ],
      "SUB_FILE_1_1" => [
        "name" => "SUB FILE 1 1',
        "new" => 1
      ]
    ]
  ],
  "FOLDER_2" => [
    "name" => "FOLDER 1",
    "new" => 0,
    "items" => [
    ...
  ],

我需要根据给定的查尔兹项目增加所有父项键“new”。
这是我需要的结果:

$tree = [
  "FOLDER_1" => [
    "name" => "FOLDER 1",

---

    "new" => 2,

---

    "items" => [
      "SUB_FOLDER_1_1" => [
        "name" => "SUB FOLDER 1 1',

---

        "new" => 1,

---

        "items" => [
          "SUB_FILE_1_1_1" => [
            "name" => "SUB FILE 1 1 1',
            "new" => 0
          ],
          "SUB_FILE_1_1_2" => [
            "name" => "SUB FILE 1 1 2',
            "new" => 1
          ],
        ]
      ],
      "SUB_FILE_1_1" => [
        "name" => "SUB FILE 1 1',
        "new" => 1
      ]
    ]
  ],
  "FOLDER_2" => [
    "name" => "FOLDER 1",
    "new" => 0,
    "items" => [
    ...
  ],

有人有主意吗?
谢谢!

5cnsuln7

5cnsuln71#

我找到了这个代码:

function completionTree(&$elem, &$parent=NULL) {
// Handle arrays that are used only as a container... if we have children but no uuid, simply descend.
    if (is_array($elem) && !isset($elem['uuid'])) {
        foreach($elem AS &$child) {
            completionTree($child, $elem);
        }
    }

    if (!empty($elem['children'])) {
        foreach ($elem['children'] AS &$child) {
            completionTree($child, $elem);
        }
    }
    if (@$parent['completed'] !== NULL) {
        $parent['completed'] = $parent['completed'] + $elem['completed'];
    }
}
// Launch recursive calculations
completionTree($tree);

它可以工作,但它是在过程中,有谁知道如何使这在oop类功能的工作?
谢谢...

vwkv1x7d

vwkv1x7d2#

有了给定的结构,

function rcount(&$array) {
    $count = 0;
    foreach ($array as $foldername => &$entry) {
        if (isset($entry['items'])) {
            $entry['new'] += rcount($entry['items']);
        }
        $count += $entry['new'];
    }
    return $count;
}

它将对直接子节点上的“new”条目求和,但在此之前,它将递归到每个子节点,并让它计算其“new”总和(并将其加到当前的new数目)。通过这种方式,内部数字被向上传输。
使用给定的(bugfixed)结构的代码的结果将给出以下输出,但请随意使用更大/更深的结构。

Array
(
    [FOLDER_1] => Array
        (
            [name] => FOLDER 1
            [new] => 2
            [items] => Array
                (
                    [SUB_FOLDER_1_1] => Array
                        (
                            [name] => SUB FOLDER 1 1
                            [new] => 1
                            [items] => Array
                                (
                                    [SUB_FILE_1_1_1] => Array
                                        (
                                            [name] => SUB FILE 1 1 1
                                            [new] => 0
                                        )

                                    [SUB_FILE_1_1_2] => Array
                                        (
                                            [name] => SUB FILE 1 1 2
                                            [new] => 1
                                        )

                                )

                        )

                    [SUB_FILE_1_1] => Array
                        (
                            [name] => SUB FILE 1 1
                            [new] => 1
                        )

                )

        )

    [FOLDER_2] => Array
        (
            [name] => FOLDER 1
            [new] => 0
            [items] => Array
                (
                )

        )

)

相关问题