1

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.

function appendToArray($arrayOfObjects) {

    $newArray = array();

    foreach ($arrayOfObjects as $object) {
        echo "<pre>".print_r($object, true)."</pre>";

        $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;
}

Here is an example of one of the objects that I start with (after json_decode of course:

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

    [geo] => stdClass Object
        (
            [latitude] => 0.0
            [longitude] => 0.0
            [name] => building name
        )
)

When this is done, what I want is this:

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

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

                [moreItems] => Array
                    (
                        [0] => more1
                        [1] => more2
                        [2] => more3
                    )

            )
)

But what I get is this:

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

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

                [moreItems] => Array
                    (
                        [0] => more1
                        [1] => more2
                        [2] => more3
                    )

            )

    )

Notice how [new_stuff] 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!

2 Answers 2

3

You solution is one string:

$object->new_stuff = $newObject;

because every $object is object and not array. And adding new property to object is done via -> and not with [].

Full code:

function appendToArray($arrayOfObjects) {

    foreach ($arrayOfObjects as $object) {
        echo "<pre>".print_r($object, true)."</pre>";

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

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

        $object->new_stuff = $newObject;
    }

    return $arrayOfObjects;
}
Sign up to request clarification or add additional context in comments.

1 Comment

I knew the answer was right in front of me. I had tried adding it to $object but couldn't make it work. I think I tried everything but that. Thanks a lot!
1

You can just array_walk and change the object inplace:

array_walk($arrayOfObjects, function(&$item) {
   $item->new_stuff = ["newItems" => ['item1', 'item2', 'item3'], etc];
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.