0

I want to merge/combine 2 arrays based on the same key/value pair.

To be more clear, what I'm searching for is kind of a join function known in MySQL. The first array should be "joined" by the second one, based on the ID, which is the key/value pair 'name'.

How can I do this?

1. ARRAY

 [0] => Array
        (
            [name] => first
            [logo] => url
            [cat] => abc
        )
[1] => Array
        (
            [name] => second
            [logo] => url
            [cat] => abc
        )

2. ARRAY

 [0] => Array
        (
            [name] => first
            [menu] => true
            [key] => value
        )

NEW ARRAY (Expexted Result):

[0] => Array
       (
           [name] => first
           [logo] => url
           [cat] => abc
           [menu] => true
           [key] => value
       )

As you an see, it's quite self-explaining. In this case, the 'name' key is like an ID (for both arrays).

1
  • to be more clear: what i'm searching for is kind of a join function known in MySQL..the first array should be "joined" by the second one, based on the ID, which is the key/value pair 'name' Commented Mar 15, 2018 at 19:29

2 Answers 2

1

If you reindex your second array by name first, it will be easier to get those values from it.

$second = array_column($second, null, 'name');

(It's okay to do this if 'name' is unique in the array. I assume that's the case since you said it's "like an ID". If 'name' isn't unique then you'll lose some rows when you reindex, because array keys have to be unique.)

Then iterate your first array and merge any corresponding values in the second array into the result.

foreach ($first as $key => $value) {
    if (isset($second[$value['name']])) {
        $result[$key] = array_merge($value, $second[$value['name']]);
    }
}

This would be like an inner join in SQL, where the result would only include rows where the value exists in both tables. If you wanted it to work more like a left join, then you'd need to merge a set of empty values for the keys in the second array if a matching name value wasn't found there.

foreach ($first as $key => $value) {
    if (isset($second[$value['name']])) {
        $result[$key] = array_merge($value, $second[$value['name']]);
    } else {
        $result[$key] = array_merge($value, ['menu' => null, 'key' => null]);
    }
}

Working example at 3v4l.org.

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

2 Comments

thanks, but some values get lost, like 'logo', also, "It looks like you wanted the result to only contain names that existed in both arrays"...no, thats not what i want. Basicly, the first array is important, the second should only add additional key/values.
@Dave18 updated the answer. I added a link to a executable example. Just let me know if I'm still not understanding it properly.
0

Try this

$mergedArray = array_merge_recursive ($firstArray,$secondArray);

$newArray = [];
foreach( $mergedArray as $subArr ) {
    $newArray = array_merge($subArr,$newArray);
}

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.