Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
    
Loop through and match on a unique and/or identifying characteristic, and store in another object? –  Jared Farrish Sep 23 '11 at 0:32
2  
You might want to consider creating a class for your object, where you can pass an array/object to the constructor or a load() method to set properties. –  Jared Farrish Sep 23 '11 at 0:33

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.