I'm trying to properly read output from a plugin I'm using for a menu editor. The output comes in the form a JSON string.
Example JSON Data:
[{"id":13},{"id":14},{"id":15,"children":[{"id":16},{"id":17},{"id":18}]}]
This is the plugin in action: http://jsfiddle.net/Av8q5/ (capped at 5 levels currently)
I'm using PHP to try to properly loop through any number of nested items, but I'm just not sure how to do this. My attempt at doing so works (but I think it's needs work) for only 2 levels deep, but I need this to expand based on any number of nested items. I'm relatively new to managing JSON strings with PHP, but here is my attempt to grab all of the ID's (my menu code has data-content as well, not just data-id's as the example shows above):
//Sample data from above
$data = '[{"id":13},{"id":14},{"id":15,"children":[{"id":16},{"id":17},{"id":18}]}]';
//Decode the JSON string to build the menu
$menu = json_decode($data, true);
//Store all of the ID's in an array
$ids = array();
//Hold the parent id to target this key when adding children to it
$y = 0;
//Start the loop through the menu array
foreach ($menu as $parentHolder) {
foreach ($parentHolder as $parentKey => $parent) {
//Checks to see when this jumps into the children array
if (!is_array($parent)) {
//Only run on the content key
if ($parentKey == "id") {
if ($parent) { $ids[] = $parent; $y++; } //Only to array if not empty
} //end check for content as key for the parents
} else {
//This is an array, which means this parent has children
foreach ($parent as $childHolder) {
foreach ($childHolder as $childKey => $child) {
//Only run on the content key
if ($childKey == "id") {
if ($child) { $ids[$y][] = $child; } //Only to array if not empty
} //end check for content as key for the children
} //End child foreach loop
} //end childholder foreach loop
} //Check if key is = content
} //end Parent loop
} //end the main loop
Output (based on example of JSON above):
While looking at this output again, I don't even think this is right... I think the child array should be appended to the 15, not on its own in the next key.
Array
(
[0] => 13
[1] => 14
[2] => 15
[3] => Array
(
[0] => 16
[1] => 17
[2] => 18
)
)
What I'm trying to do is properly loop through some JSON like this...
Example:
[{"id":13,"children":[{"id":20,"children":[{"id":21},{"id":22}]}]},{"id":15,"children":[{"id":16,"children":[{"id":18},{"id":14},{"id":19}]},{"id":17},{"id":23}]}]
In this example, there are children within children, how to I properly get this out into a nested array in PHP while keeping in mind that this menu could go as deep as 10 nested lists?
One solution would be to write a recursive function. A function that calls itself. This is a rough example using your example code. I broke your output code into a function. In the case that the nested content is an array, simply call the function and pass in the nested data as the parameter.
function output( $menu )
{
//Start the loop through the menu array
foreach ($menu as $parentHolder) {
foreach ($parentHolder as $parentKey => $parent) {
//Checks to see when this jumps into the children array
if (!is_array($parent)) {
//Only run on the content key
if ($parentKey == "id") {
if ($parent) { $ids[] = $parent; $y++; } //Only to array if not empty
} //end check for content as key for the parents
} else {
//This is an array, which means this parent has children
foreach ($parent as $childHolder) {
foreach ($childHolder as $childKey => $child) {
// recursive function call here
output( $child )
} //end check for content as key for the children
} //End child foreach loop
} //end childholder foreach loop
} //Check if key is = content
} //end Parent loop
} //end the main loop
}
Hello friends ، A convenient code
$d=json_decode('[{"id":1},{"id":2,"children":[{"id":3},{"id":4},{"id":5,"children":[{"id":6},{"id":7},{"id":8}]},{"id":9},{"id":10}]},{"id":11},{"id":12}]');
output($d);
function output($menu,$parent=0)
{
foreach ($menu as $handle)
{
if(isset($handle->children))
{
echo 'node : '.$handle->id.' - Pid : '.$parent.'<br>';
// Insert into DataBase Code (menuId = $handle->id and ParentId = $parent)
output($handle->children,$handle->id);
}
else if(isset($handle->id))
{
echo 'node : '.$handle->id.' - Pid : '.$parent.'<br>';
// Insert into DataBase Code (menuId = $handle->id and ParentId = $parent)
}
}
}
node : 1 - pid : 0
node : 2 - pid : 0
node : 3 - pid : 2
node : 4 - pid : 2
node : 5 - pid : 2
node : 6 - pid : 5
node : 7 - pid : 5
node : 8 - pid : 5
node : 9 - pid : 2
node : 10 - pid : 2
node : 11 - pid : 0
node : 12 - pid : 0