0

I have two arrays and I want to combine them together

1) first look like this:

[11] => Array
    (
        [id] => 11
        [name] => test
    )
[12] => Array
    (
        [id] => 12
        [name] => test1
    )

2) second array look like this:

[0] => Array
    (
        [offer_id] => 11
        [countries] => Array
            (
                [SA] => Array
                    (
                        [id] => 682                           
                    )
            )
    )

[1] => Array
    (
        [offer_id] => 12
        [countries] => Array
            (
                [KW] => Array
                    (
                        [id] => 414                           
                    )
            )
    )

I want this result. How is it possible can any one provide solution for same?

[11] => Array
    (
        [id] => 11
        [name] => test
        [countries] => Array
            (
                [SA] => Array
                    (
                        [id] => 682                           

                    )

            )

    )
[12] => Array
    (
        [id] => 12
        [name] => test
        [countries] => Array
            (
                [KW] => Array
                    (
                        [id] => 414                            
                    )
            )
    )

Thank you for the help!

1
  • please update your question with formatted php array and also format your expected output array Commented Mar 29, 2017 at 11:56

1 Answer 1

1

Try this:

foreach ($array1 as &$arr1) {
    $offer_id = $arr1['id'];    // Search for this offer_id in array 2
    $match = array_filter($array2, function($v) use ($offer_id){
        return $v['offer_id'] == $offer_id;  // Return matching offer id
    });
   $arr1['countries'] = current($match)['countries']; // Assign matched country to array
}
Sign up to request clarification or add additional context in comments.

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.