I have the following data inside an multi level arrarray
1: id: 41, parent: null, name: lucas
2: id: 52, parent: null, name: george
3: id 98: parent: 41, name: julian
...
I need to loop through this array and a 'childrens'
key and value to the parent while summing +1 to this value every time i run over an entry that has 'parent' not set to null. How to achieve this?
1: id: 41, parent: null, name: lucas, children: 1
I tried this inside a foreach
foreach($post as $parsedPost) {
if($parsedPost['parent'] > 0){
$idChild = $parsedPost['id'];
$idParent = $parsedPost['parent'];
$post[$idParent]["childrens"]++;
}
}
Of course, it returns a notice because the index ['childrens']
does not exist. Also, it's adding the childrens:value
to the first children and not to the parent.
Why?