Add Array to Existing Object
<p>This seems pretty easy, but I can't figure it out. I have an existing array of objects that was json_decoded. I need to iterate over those objects, append a new array to each, and then put the overall array back together. I can't get the new array to append in the correct place.</p>

<pre><code>function appendToArray($arrayOfObjects) {

    $newArray = array();

    foreach ($arrayOfObjects as $object) {
        echo "&lt;pre&gt;".print_r($object, true)."&lt;/pre&gt;";

        $newItems = array('item1', 'item2', 'item3');
        $moreItems = array('more1', 'more2', 'more3');

        $newObject = array();
        $newObject["newItems"] = $newItems;
        $newObject["moreItems"] = $moreItems;

        $rebuiltObject = array();
        array_push($rebuiltObject, $object);
        $rebuiltObject['new_stuff'] = $newObject;
        // $rebuiltObject[0]['new_stuff'] = $newObject;

        array_push($newArray, $newObject);
    }

     return $newArray;
}
</code></pre>

<p>Here is an example of one of the objects that I start with (after <code>json_decode</code> of course:</p>

<pre><code>0 =&gt; stdClass Object
(
    [code] =&gt; some code
    [first_name] =&gt; First
    [last_name] =&gt; Last
    [urls] =&gt; stdClass Object
        (
            [news] =&gt; http://www.somesite.com/news
            [info] =&gt; http://www.somesite.com/info
        )

    [geo] =&gt; stdClass Object
        (
            [latitude] =&gt; 0.0
            [longitude] =&gt; 0.0
            [name] =&gt; building name
        )
)
</code></pre>

<p>When this is done, what I want is this:</p>

<pre><code>0 =&gt; stdClass Object
(
    [code] =&gt; some code
    [first_name] =&gt; First
    [last_name] =&gt; Last
    [urls] =&gt; stdClass Object
        (
            [news] =&gt; http://www.somesite.com/news
            [info] =&gt; http://www.somesite.com/info
        )

    [geo] =&gt; stdClass Object
        (
            [latitude] =&gt; 0.0
            [longitude] =&gt; 0.0
            [name] =&gt; building name
        )
     [new_stuff] =&gt; Array
            (
                [newItems] =&gt; Array
                    (
                        [0] =&gt; item1
                        [1] =&gt; item2
                        [2] =&gt; item3
                    )

                [moreItems] =&gt; Array
                    (
                        [0] =&gt; more1
                        [1] =&gt; more2
                        [2] =&gt; more3
                    )

            )
)
</code></pre>

<p>But what I get is this:</p>

<pre><code>[0] =&gt; stdClass Object
(
    [code] =&gt; some code
    [first_name] =&gt; First
    [last_name] =&gt; Last
    [urls] =&gt; stdClass Object
        (
            [news] =&gt; http://www.somesite.com/news
            [info] =&gt; http://www.somesite.com/info
        )

    [geo] =&gt; stdClass Object
        (
            [latitude] =&gt; 0.0
            [longitude] =&gt; 0.0
            [name] =&gt; building name
        )
),
[new_stuff] =&gt; Array
            (
                [newItems] =&gt; Array
                    (
                        [0] =&gt; item1
                        [1] =&gt; item2
                        [2] =&gt; item3
                    )

                [moreItems] =&gt; Array
                    (
                        [0] =&gt; more1
                        [1] =&gt; more2
                        [2] =&gt; more3
                    )

            )

    )
</code></pre>

<p>Notice how <code>[new_stuff]</code> is outside of the original object. I need to get it inside. Everything else I've tried crashes and I'm totally out of ideas. Can anyone see how I can do this? Thank you!</p>