1

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.

2
  • $name['children'][]=array(...). You are putting 'store number 10' in the wrong spot. You probably want $name['children'][0]['children'][] = array(...).
    – gen_Eric
    Commented Sep 3, 2015 at 21:31
  • I think it would a lot easier for you to visualize and debug this sort of thing if you print out the array itself rather than encoding it as json. Like this: echo '<pre>'; print_r($name); echo '</pre>'; Commented Sep 3, 2015 at 21:43

2 Answers 2

2

It looks like you want this:

$name['children'][0]['children'][] = array(...);

This would reference the first child of the root element (index 0), then proceed like you did previously by adding to that elements children array.

2
  • Thanks. I think I see recursion in my future.
    – johnny
    Commented Sep 3, 2015 at 21:33
  • @johnny: It certainly looks that way, since you seem to be working with a tree structure here.
    – jwueller
    Commented Sep 3, 2015 at 21:35
1
$name=array('name'=>'My Company LLC ->');
$name['children']=array(array('name'=>'District of the Stores', 'children'=>array(array('name'=>'johnny1'), array('name'=>'jonny2'), array('name'=>'store number 10', 'children'=>array(array('name'=>'johnny3'), array('name'=>'johnny4'))))));
    echo '<pre>';
echo json_encode($name, JSON_PRETTY_PRINT | JSON_NUMERIC_CHECK);
echo '</pre>';

Here is the output

{
    "name": "My Company LLC ->",
    "children": [
        {
            "name": "District of the Stores",
            "children": [
                {
                    "name": "johnny1"
                },
                {
                    "name": "jonny2"
                },
                {
                    "name": "store number 10",
                    "children": [
                        {
                            "name": "johnny3"
                        },
                        {
                            "name": "johnny4"
                        }
                    ]
                }
            ]
        }
    ]
}

If you want to keep the lines of code separate you can do the following:

$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'][0]['children'][] = array('name'=>'store number 10', 'children'=>array(array('name'=>'johnny3'), array('name'=>'johnny4')));
    echo '<pre>';
echo json_encode($name, JSON_PRETTY_PRINT | JSON_NUMERIC_CHECK);
echo '</pre>';

Also, it is probably a lot easier to debug this sort of thing by looking at the array instead of encoding into json. here is your original output as an array, which really shows you what's going on better.

$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>';
print_r($name);
echo '</pre>';

And the output is:

Array
(
    [name] => My Company LLC ->
    [children] => Array
        (
            [0] => Array
                (
                    [name] => District of the Stores
                    [children] => Array
                        (
                            [0] => Array
                                (
                                    [name] => johnny1
                                )

                            [1] => Array
                                (
                                    [name] => jonny2
                                )

                        )

                )

            [1] => Array
                (
                    [name] => store number 10
                    [children] => Array
                        (
                            [0] => Array
                                (
                                    [name] => johnny3
                                )

                            [1] => Array
                                (
                                    [name] => jonny4
                                )

                        )

                )

        )

)

Not the answer you're looking for? Browse other questions tagged or ask your own question.