Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
284 views
in Technique[技术] by (71.8m points)

php - I need merge 2 multidimensional arrays

Array #1

Array
(
    [0] => Array
        (
            [id] => 261
            [ownerName] => George Dwell
            [Department] => Information Technology
        )

    [1] => Array
        (
            [id] => 262
            [ownerName] => Dave Duken
            [Department] => Product
        )

    [2] => Array
        (
            [id] => 258
            [ownerName] => Ruby Richards
            [Department] => Services
        )

    [3] => Array
        (
            [id] => 263
            [ownerName] => Charles Mavric
            [Department] => Services
        )

    [4] => Array
        (
            [id] => 264
            [ownerName] => Austin Mayer
            [Department] => Sales
        )

    [5] => Array
        (
            [id] => 265
            [ownerName] => Aleo Kirb
            [Department] => Sales
        )

    [6] => Array
        (
            [id] => 267
            [ownerName] => dsrana4 dsrana4
            [Department] => 
        )

    [7] => Array
        (
            [id] => 260
            [ownerName] => Jennifer Jackson
            [Department] => Marketing
        )

    [8] => Array
        (
            [id] => 266
            [ownerName] => Allan Francis
            [Department] => Product
        )

    [9] => Array
        (
            [id] => 2
            [ownerName] => George Dwell
            [Department] => Information Technology
        )

    [10] => Array
        (
            [id] => 1
            [ownerName] => George Dwell
            [Department] => Information Technology
        )

)

Array 2#

Array
(
    [0] => stdClass Object
        (
            [id] => 264
            [total_time] => 0.7
        )

    [1] => stdClass Object
        (
            [id] => 2
            [total_time] => 1.25
        )

    [2] => stdClass Object
        (
            [id] => 261
            [total_time] => 2
        )

    [3] => stdClass Object
        (
            [id] => 258
            [total_time] => 1.4
        )

)

I want new array as same like this..

New Array #

Array
(
    [0] => Array
        (
            [id] => 261
            [ownerName] => George Dwell
            [Department] => Information Technology
            [total_time] => 2
        )

    [1] => Array
        (
            [id] => 262
            [ownerName] => Dave Duken
            [Department] => Product
            [total_time] => 0
        )

    [2] => Array
        (
            [id] => 258
            [ownerName] => Ruby Richards
            [Department] => Services
            [total_time] => 1.4
        )

    [3] => Array
        (
            [id] => 263
            [ownerName] => Charles Mavric
            [Department] => Services
            [total_time] => 0
        )

    [4] => Array
        (
            [id] => 264
            [ownerName] => Austin Mayer
            [Department] => Sales
            [total_time] => 0.7
        )

    [5] => Array
        (
            [id] => 265
            [ownerName] => Aleo Kirb
            [Department] => Sales
            [total_time] => 0
        )

    [6] => Array
        (
            [id] => 267
            [ownerName] => dsrana4 dsrana4
            [Department] => 
            [total_time] => 0
        )

    [7] => Array
        (
            [id] => 260
            [ownerName] => Jennifer Jackson
            [Department] => Marketing
            [total_time] => 0
        )

    [8] => Array
        (
            [id] => 266
            [ownerName] => Allan Francis
            [Department] => Product
            [total_time] => 0
        )

    [9] => Array
        (
            [id] => 2
            [ownerName] => George Dwell
            [Department] => Information Technology
            [total_time] => 1.25
            [total_time] => 0
        )

    [10] => Array
        (
            [id] => 1
            [ownerName] => George Dwell
            [Department] => Information Technology
            [total_time] => 0
        )

)

How can do this?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It would be hard to merge the array's like this. One suggestion is to create the $new_array and key the values by the id of $array_1. Then you can loop over $array_2 and update $new_array using the id from the object in $array_2.

$new_array = [];

foreach ($array_1 as $item) {
    $new_array[$item['id']] = $item;
}

foreach ($array_2 as $item) {
    // Perhaps first make sure that the key exists if array_2 
    // could have a total_time that will not be in array_1
    $new_array[$item->id]['total_time'] = $item->total_time;
}

// In case you must have your keys starting from 0
// you can uncomment the call to array_values.
// $new_array = array_values($new_array);
print_r($new_array);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...