More specifically, I'm looking to pass a parent ID to the children, more over, if the children have grandchildren, I'd need to pass the ID of the child to the grandchildren... and so on and so forth for an unlimited number of nested items (hence the use of a recursive function).
Note: The post looks long, but it's mostly just output data! I'm pretty sure there is a simple solution to this but I can't figure it out after hours upon hours of attempts.
So far I have this:
<?php
session_start();
$data = '[{"id":13,"content":"This is some content"},{"id":14,"content":"This is some content"},{"id":15,"content":"This is some content","children":[{"id":16,"content":"This is some content"},{"id":17,"content":"This is some content"},{"id":18,"content":"This is some content","children":[{"id":19,"content":"This is some content","children":[{"id":20,"content":"This is some content","children":[{"id":21,"content":"This is some content"},{"id":22,"content":"This is some content"},{"id":23,"content":"This is some content"}]}]}]}]},{"id":24,"content":"This is some content"},{"id":25,"content":"This is some content"}]';
$menu = json_decode($data, true);
$depth = 0;
function getData($array, $key, $depth) {
if (!is_array($array)) {
$depth = $depth/2;
$depthHolder = $depth;
if ($key == "id") {
//I thought maybe I could write something in here to this effect, but was unsuccessful :(
}
while ($depth != 1) {
echo " ";
$depth--;
}
echo $depthHolder . ' - ' . $key . ' : ' . $array . '<br/> ';
} else {
$depth++;
}
foreach($array as $key => $v) {
getData($v, $key, $depth);
}
}
getData($menu, '', $depth);
?>
Which outputs this (currently the numbers in front just show the depth of the nested items):
1 - id : 13
1 - content : This is some content
1 - id : 14
1 - content : This is some content
1 - id : 15
1 - content : This is some content
2 - id : 16
2 - content : This is some content
2 - id : 17
2 - content : This is some content
2 - id : 18
2 - content : This is some content
3 - id : 19
3 - content : This is some content
4 - id : 20
4 - content : This is some content
5 - id : 21
5 - content : This is some content
5 - id : 22
5 - content : This is some content
5 - id : 23
5 - content : This is some content
1 - id : 24
1 - content : This is some content
1 - id : 25
1 - content : This is some content
I tried to use sessions but I still couldn't figure it out. What I'm looking for is shown in the example output below. You'll notice that the ID's in front of the rows have changed to show the previous parents ID and holds it until the next nested item shows up (0 represents 'no parent').
0 - id : 13
0 - content : This is some content
0 - id : 14
0 - content : This is some content
0 - id : 15
0 - content : This is some content
15 - id : 16
15 - content : This is some content
15 - id : 17
15 - content : This is some content
15 - id : 18
15 - content : This is some content
18 - id : 19
18 - content : This is some content
19 - id : 20
19 - content : This is some content
20 - id : 21
20 - content : This is some content
20 - id : 22
20 - content : This is some content
20 - id : 23
20 - content : This is some content
0 - id : 24
0 - content : This is some content
0 - id : 25
0 - content : This is some content
Sorry about the lengthy post! Thanks for reading, hopefully one of you geniuses can help me out. I will be grateful as I've tried for 6 hours to figure this one little issue out.