PHP如何使用数组数组合并一个对象数组

时间:2021-07-05 12:18:18

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.

首先,对于长篇解释感到遗憾。我在PHP中有两个数组。第一个数组是一个对象数组。第二个数组是一个数组数组。基本上,我想循环,并将对象与其匹配的数组合并,并返回一个合并的对象。

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

请参阅以下对象结构数组的print_r():

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:

请参阅以下数组结构数组的print_r():

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 个解决方案

#1


3  

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.

如果可能的话,默认情况下让代码返回带索引的数组,可以删除第一个foreach循环。

Hope that helps.

希望有所帮助。

#1


3  

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.

如果可能的话,默认情况下让代码返回带索引的数组,可以删除第一个foreach循环。

Hope that helps.

希望有所帮助。