Using a recursive PHP function to parse a JSON string, how can I pass data from one item to the next? -
more specifically, i'm looking pass parent id children, more over, if children have grandchildren, i'd need pass id of child grandchildren... , on , forth unlimited number of nested items (hence use of recursive function).
note: post looks long, it's output data! i'm pretty sure there simple solution can't figure out after hours upon hours of attempts.
so far have this:
<?php session_start(); $data = '[{"id":13,"content":"this content"},{"id":14,"content":"this content"},{"id":15,"content":"this content","children":[{"id":16,"content":"this content"},{"id":17,"content":"this content"},{"id":18,"content":"this content","children":[{"id":19,"content":"this content","children":[{"id":20,"content":"this content","children":[{"id":21,"content":"this content"},{"id":22,"content":"this content"},{"id":23,"content":"this content"}]}]}]}]},{"id":24,"content":"this content"},{"id":25,"content":"this 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 write in here effect, unsuccessful :( } while ($depth != 1) { echo " "; $depth--; } echo $depthholder . ' - ' . $key . ' : ' . $array . '<br/> '; } else { $depth++; } foreach($array $key => $v) { getdata($v, $key, $depth); } } getdata($menu, '', $depth); ?> which outputs (currently numbers in front show depth of nested items):
1 - id : 13 1 - content : content 1 - id : 14 1 - content : content 1 - id : 15 1 - content : content 2 - id : 16 2 - content : content 2 - id : 17 2 - content : content 2 - id : 18 2 - content : content 3 - id : 19 3 - content : content 4 - id : 20 4 - content : content 5 - id : 21 5 - content : content 5 - id : 22 5 - content : content 5 - id : 23 5 - content : content 1 - id : 24 1 - content : content 1 - id : 25 1 - content : content i tried use sessions still couldn't figure out. i'm looking shown in example output below. you'll notice id's in front of rows have changed show previous parents id , holds until next nested item shows (0 represents 'no parent').
0 - id : 13 0 - content : content 0 - id : 14 0 - content : content 0 - id : 15 0 - content : content 15 - id : 16 15 - content : content 15 - id : 17 15 - content : content 15 - id : 18 15 - content : content 18 - id : 19 18 - content : content 19 - id : 20 19 - content : content 20 - id : 21 20 - content : content 20 - id : 22 20 - content : content 20 - id : 23 20 - content : content 0 - id : 24 0 - content : content 0 - id : 25 0 - content : content sorry lengthy post! reading, 1 of geniuses can me out. grateful i've tried 6 hours figure 1 little issue out.
perhaps approach clearer:
function getdata($parentkey,$array,$depth) { foreach ($array $v) { print "$depth $parentkey id: {$v['id']}<br>$depth $parentkey content:{$v['content']}<br>"; if (isset($v['children'])) getdata($v['id'],$v['children'],$depth." "); } } getdata(0,$menu,'');
Comments
Post a Comment