PHP如何合并对象的数组,数组的数组 [英] PHP How To Merge An Array Of Objects WIth An Array Of Arrays

查看:375
本文介绍了PHP如何合并对象的数组,数组的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,遗憾的过长的解释。我在PHP两个数组。第一阵列是对象的数组。第二阵列是数组的数组。基本上,我想通过循环,与其配套的数组合并对象,并返回一个合并对象。

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.

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

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
            )

    )

)

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

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?

推荐答案

如果您可以通过齿轮或一些独特价值指数的阵列,这将是一个容易得多。

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

如果能够得到您的code返回与默认情况下,该指数的阵列,可以删除第一个foreach循环。

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

希望有所帮助。

这篇关于PHP如何合并对象的数组,数组的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆