PHP How To Merge An Array Of Objects WIth An Array

2020-03-26 12:12发布

问题:

First, sorry for the lengthly explanation. I have two arrays in PHP. The first array is an array of objects. The second array is an array of arrays. Basically, I want to loop through, and merge the object with its matching array, and return a merged object.

See the following print_r() of the array of objects structures:

Array
(
[0] => stdClass Object
    (
        [gear] => helloworld
        [status] => running
        [started] => 40 Minutes Ago
        [start] => index.js
        [route] => 127.0.0.1:3000
        [parameters] => Array
            (
            )

    )

[1] => stdClass Object
    (
        [gear] => test
        [status] => stopped
        [started] => 
        [start] => index.js
        [route] => 
        [parameters] => Array
            (
            )

    )

[2] => stdClass Object
    (
        [gear] => test2
        [status] => stopped
        [started] => 
        [start] => index.js
        [route] => 
        [parameters] => Array
            (
                [0] => first
                [1] => second
                [2] => third
            )

    )

)

See the following print_r() of the array of arrays structures:

Array
(
[0] => Array
    (
        [gear] => helloworld
        [machine_id] => E6z5ekvQ
        [created_by] => 10010
        [modified_by] => 10010
        [created] => 2011-09-22T16:30:11-07:00
        [modified] => 2011-09-22T16:30:11-07:00
    )

[1] => Array
    (
        [gear] => test
        [machine_id] => E6z5ekvQ
        [created_by] => 10010
        [modified_by] => 10010
        [created] => 2011-09-22T16:44:25-07:00
        [modified] => 2011-09-22T16:44:25-07:00
    )

[2] => Array
    (
        [gear] => test2
        [machine_id] => E6z5ekvQ
        [created_by] => 10010
        [modified_by] => 10010
        [created] => 2011-09-22T16:45:43-07:00
        [modified] => 2011-09-22T16:45:43-07:00
    )

)

So basically the matching key for both is gear. So we should match the gear from the first object, with the second gear in the array, and return something like:

stdClass Object
    (
        [gear] => helloworld
        [status] => running
        [started] => 40 Minutes Ago
        [start] => index.js
        [route] => 127.0.0.1:3000
        [parameters] => Array
            (
            )
        [machine_id] => E6z5ekvQ
        [created_by] => 10010
        [modified_by] => 10010
        [created] => 2011-09-22T16:30:11-07:00
        [modified] => 2011-09-22T16:30:11-07:00
    )

Notice, that the gear is merged into one property of the object, obviously gear does not appear twice. Ideas?

回答1:

If you could index the array by gear or some unique value, it would be a lot easier.

$indexed = array();

// create an array using 'gear' as the index
foreach($arrayValue as $value) {
    $indexed[$value['gear']] = $value;
}

// loop over each object
foreach($objectArray as $obj) {
    $value = $indexed[$obj->gear]; // find the corresponding array
    foreach($value as $name => $val) {
        $obj->$name = $val; // assign each array index/value pair to the object
    }
}

If possible to get your code to return the array with the index by default, you can remove the first foreach loop.

Hope that helps.