skip to Main Content

I everyone !
I need some help here…

Im trining to achieve a recursive function that can increment the key "new" in all parents, according to respective "items" childs.

I have an array that looks like this :

$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" => [
    ...
  ],

I need to increment all parents items key "new" according to given childs items.

There is the result i need :

$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" => [
    ...
  ],

Does anyone have an idea ?

Thanks !!!!

2

Answers


  1. Chosen as BEST ANSWER

    I found this code :

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

    It works but it's in procedural, doe's anyone know how the make this work in oop class function ?

    Thanks...


  2. With the structure given, this should do the trick

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

    It will sum the "new" entries on the direct children, but before that, it will recurse into each child and let this calculate its "new" sum (and add it up to the current number of new). By this, the inner numbers are tranported upwards.

    The result of the code with your given (bugfixed) structure will give the following output, but feel free to use larger/deeper structures.

    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
                    (
                    )
    
            )
    
    )
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search