Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge two objects in php

Tags:

object

php

I use an API that returns me to an email address given an array like this:

stdClass Object
(
    [status] => OK
    [contact] => stdClass Object
        (
            [id] => 0000000
            [email] => [email protected]
            [last_activity] => 1362131446
            [last_update] => 0
            [created_at] => 1356617740
            [sent] => 5
            [open] => 1
            [click] => 1
            [spam] => 0
            [bounce] => 0
            [blocked] => 0
            [queued] => 0
        )
[lists] => Array
        (
            [0] => stdClass Object
                (
                    [active] => 1
                    [unsub] => 1
                    [unsub_at] => 1363078528
                )

        )

)

how to merge info [contact] with [lists] [0] in a single object?

Thank you for your help

like image 522
Christophe Martin Avatar asked Mar 18 '13 09:03

Christophe Martin


People also ask

How to combine 2 objects in PHP?

Approach 1: Convert object into data array and merge them using array_merge() function and convert this merged array back into object of class stdClass. Note: While merging the objects using array_merge(), elements of array in argument1 are overwritten by elements of array in argument2.

How do I combine two objects in laravel?

You could simply use array_merge(firstObject,secondObject) function.


1 Answers

$info = yourstuff;
$arrContact = (array) $info->contact;
$arrList = (array) $info->lists[0];
$merged = array_merge($arrContact, $arrList);
var_dump($merged, 'have fun');

Quite trivial ;)

like image 189
Joshua Avatar answered Sep 19 '22 12:09

Joshua