I cant get my head around how to do this.
Consider I have the following php array
Array
(
[0] => Array
(
[cat_id] => 2
[parent_id] => 1
[Title] => Default Category
[Product Count] => 0
[children] => Array
(
[0] => Array
(
[cat_id] => 4
[parent_id] => 2
[Title] => computers & electronics
[Product Count] => 5
[children] => Array
(
[0] => Array
(
[cat_id] => 5
[parent_id] => 4
[Title] => projectors
[Product Count] => 2
[children] => Array
(
[0] => Array
(
[cat_id] => 6
[parent_id] => 5
[Title] => film projectors
[Product Count] => 1
)
[1] => Array
(
[cat_id] => 7
[parent_id] => 4
[Title] => projector cases
[Product Count] => 0
children] => Array
(
[0] => Array
(
[cat_id] => 8
[parent_id] => 7
[Title] => leather cases
[Product Count] => 10
I want to somehow iterate over each array element and total up the Product Count element based on its on value and all child values (which could be n levels deep)
The end result I am looking for is
Array
(
[0] => Array
(
[cat_id] => 2
[parent_id] => 1
[Title] => Default Category
[Product Count] => 18
[children] => Array
(
[0] => Array
(
[cat_id] => 4
[parent_id] => 2
[Title] => computers & electronics
[Product Count] => 18
[children] => Array
(
[0] => Array
(
[cat_id] => 5
[parent_id] => 4
[Title] => projectors
[Product Count] => 3
[children] => Array
(
[0] => Array
(
[cat_id] => 6
[parent_id] => 5
[Title] => film projectors
[Product Count] => 1
)
[1] => Array
(
[cat_id] => 7
[parent_id] => 4
[Title] => projector cases
[Product Count] => 10
children] => Array
(
[0] => Array
(
[cat_id] => 8
[parent_id] => 7
[Title] => leather cases
[Product Count] => 10
can anyone help please? Speed or efficiency is not really a major issue as this will be run once a day at 3 in the morning so no-one will be waiting for it to finish its work!!