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
I found this code :
It works but it's in procedural, doe's anyone know how the make this work in oop class function ?
Thanks...
With the structure given, this should do the trick
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.