0

I want to merge two arrays in order to get the data as per my requirement.

I am posting my result, please have a look.

First array:

Array
(
    [0] => Array
        (
            [km_range] => 300
            [id] => 2
            [car_id] => 14782

        )
     [1] => Array
        (
            [km_range] => 100
            [id] => 3
            [car_id] => 14781

        )    

    [2] => Array
        (
            [km_range] => 300
            [id] => 4
            [car_id] => 14783

        )

)

Second array:

Array
(
    [0] => Array
        (
            [user_id] => 9c2e00508cb28eeb1023ef774b122e86
            [car_id] => 14783
            [status] => favourite
        )

)

I want to merge the second array into the first one, where the value at key car_id matches the equivalent value; otherwise it will return that field as null.

Required output:

<pre>Array
(
    [0] => Array
        (
            [km_range] => 300
            [id] => 2
            [car_id] => 14782


        )
     [1] => Array
        (
            [km_range] => 100
            [id] => 3
            [car_id] => 14781



        )    

    [2] => Array
        (
            [km_range] => 300
            [id] => 4
            [car_id] => 14783
            [fav_status] => favourite

        )

)
2
  • What have you tried so far? Is there a question about code you wrote that isn't working? If so please include that code in a Minimal, Complete, and Verifiable example. Commented Jan 9, 2017 at 16:08
  • array_merge is pretty well documented Commented Jan 9, 2017 at 17:02

2 Answers 2

2

Since the merge is so specific I would try something like this:

foreach ($array1 as $index => $a1):
    foreach ($array2 as $a2):
       if ($a1['car_id'] == $a2['car_id']):
           if ($a2['status'] == "favourite"): 
               $array1[$index]['fav_status'] = "favourite";
           endif;
       endif;
    endforeach;
endforeach;

You might be able to optimize the code more but this should be very easy to follow...

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you , Previously I have tried in this way , but did not get success. Now I found my mistakes ,Thanks a lot for your valuable answer.
1

Another way to achieve this without using the index syntax is to reference the array elements in the foreach by-reference by prepending the ampersand operator:

foreach($firstArray as &$nestedArray1) {
    foreach($secondArray as $nestedArray2) {
        if ($nestedArray1['car_id'] == $nestedArray2['car_id']) {
            $nestedArray1['fav_status'] = $nestedArray2['status'];
        }
    }
}

You can see it in action in this Playground example.

Technically you asked about merging the arrays. While the keys would be different between the input arrays and the desired output (i.e. "status" vs "fav_status"), array_merge() can be used to merge the arrays.

if ($nestedArray1['car_id'] == $nestedArray2['car_id']) {
    $nestedArray1 = array_merge($nestedArray1, $nestedArray2);
}

Playground example.

Additionally the union operators (i.e. +, +=) can be used.

If you want to append array elements from the second array to the first array while not overwriting the elements from the first array and not re-indexing, use the + array union operator1

if ($nestedArray1['car_id'] == $nestedArray2['car_id']) {
    $nestedArray1 += nestedArray1;
}

Playground example.


1http://php.net/manual/en/function.array-merge.php#example-5587

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.