开发者

php merging two arrays into one

I have two arrays that need to be merged together and trying to figure out the correct way of doing it.

this is the first array

Array
    (
        [IndividualOutmsg] => Array
            (
                [0] => Array
                    (
                        [user_id] => 3
                        [number] => 414566765
                        [msg] => some message
                    )

                [1] => Array
                    (
                        [user_id] => 3
                        [number] => 410335509
                        [msg] => any message
                    )

            )

    )

this is the second array:

Array
    (
        [0] => Array
            (
                [0] => OK
                [1] 开发者_运维技巧=> 0
                [2] => d142b46128b869d0
                [3] => 6178977058476937
            )

        [1] => Array
            (
                [0] => OK
                [1] => 0
                [2] => 60f403f4e243e684
                [3] => 6198708709873543
            )
    )

what i want to get is this:

Array
(
    [IndividualOutmsg] => Array
        (
            [0] => Array
                (
                    [user_id] => 3
                    [number] => 414566765
                    [msg] => some message
                    [sms_status] => OK
                    [error_code] => 0
                    [msg_id] => d142b46128b869d0
                    [msg_id_2] => 6178977058476937

                )

            [1] => Array
                (
                    [user_id] => 3
                    [number] => 410335509
                    [msg] => any message
                    [sms_status] => OK
                    [error_code] => 0
                    [msg_id] => 60f403f4e243e684
                    [msg_id_2] => 6198708709873543

                )

        )

)


In that format, you really have to do a lot of the legwork yourself and can't just use array_merge to combine the arrays. It would have to be a more custom job, like so:

$count = count($second_array);
for($i=0; $i<$count; $i++){
   $first_array['IndividualOutmsg'][$i]['sms_status'] = $second_array[0];
   $first_array['IndividualOutmsg'][$i]['error_code'] = $second_array[1];
   $first_array['IndividualOutmsg'][$i]['msg_id'] = $second_array[2];
   $first_array['IndividualOutmsg'][$i]['msg_id2'] = $second_array[3];
}

If you were to output the second array with the associative keys set, it would be much easier to combine them using array_merge, provided the keys didn't conflict.

$count = count($second_array);
for($i=0; $i<$count; $i++){
   $first_array['IndividualOutmsg'][$i] = 
            array_merge($first_array['IndividualOutmsg'][$i], $second_array[$i]);
}


http://au.php.net/manual/en/function.array-merge.php

Array merge might be what you're looking for...

Though you'll need to probably write a loop or function that can get to the right place in your multi-dimensional array, perform the merge and also change the relevant keys.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜