I have read lots of answers on SO, but I cannot figure out how to make them work with my problem.
This is what I have:
{
"name": "My Company LLC ->",
"children": [
{
"name": "District of the Stores",
"children": [
{
"name": "johnny1"
},
{
"name": "jonny2"
}
]
}, //I don't want my array to end here
{
"name": "store number 10",
"children": [
{
"name": "johnny3"
},
{
"name": "jonny4"
}
]
}
]
}
This is what I want.
{
"name": "My Company LLC ->",
"children": [
{
"name": "District of the Stores",
"children": [
{
"name": "johnny1"
},
{
"name": "jonny2"
},
{
"name": "store number 10",
"children": [
{
"name": "johnny3"
},
{
"name": "jonny4"
}
]
}
]
}
]
}
Here is what I tried to do it with:
$name=array('name'=>'My Company LLC ->');
$name['children']=array(array('name'=>'District of the Stores', 'children'=>array(array('name'=>'johnny1'), array('name'=>'jonny2'))));
$name['children'][]=array('name'=>'store number 10', 'children'=>array(array('name'=>'johnny3'), array('name'=>'jonny4')));
echo '<pre>';
echo json_encode($name, JSON_PRETTY_PRINT | JSON_NUMERIC_CHECK);
echo '</pre>';
I know that I am inserting an array at the end of the children array which is causing my second array problem in the first example, but I don't know how to put the second children array back into the first one back in the original. This is part of a big list from a database, but I'm stuck here. I cannot see where the SO answers I've found help me to insert the second array.
$name['children'][]=array(...)
. You are putting 'store number 10'
in the wrong spot. You probably want$name['children'][0]['children'][] = array(...)
.