Merging two multidimensional associative arrays I just wrote a function that will loop through two multidimensional associative arrays and merge them together based on the key names and their values. While I'm satisfied that this function works, I have a strong feeling that it can be improved. Can I get some feedback?

&lt;?php

function array_merge_defaults (array &amp;$array1, array &amp;$array2, $keyField)
{
    $merged = $array1;

    foreach ($array2 as $key =&gt; &amp;$value)
    {
        $valueMerged = false;

        foreach ($merged as $mergedKey =&gt; &amp;$item)
        {
            if (is_array($item) &amp;&amp; array_key_exists($keyField, $item) &amp;&amp; $item[$keyField] == $value[$keyField])
            {
                $item = array_merge($item, $value);
                $valueMerged = true;

                break;
            }
            else if ($mergedKey == $key)
            {
                if (is_numeric($mergedKey))
                {
                    $merged[] = $value;
                }
                else
                {
                    $item = $value;
                }

                $valueMerged = true;

                break;
            }
        }

        if (!$valueMerged)
        {
            $merged[$key] = $value;
        }
    }

    return $merged;
}

$defaults = array(
    array(
        "name" =&gt; "foo",
        "level" =&gt; 5,
        "meta" =&gt; array(
            "value" =&gt; 1
        )
    ),
    array(
        "name" =&gt; "bar",
        "level" =&gt; 6
    )
);

$custom = array(
    array(
        "name" =&gt; "qux",
        "date" =&gt; "2016-01-01"
    ),
    array(
        "name" =&gt; "tux",
        "date" =&gt; "2016-01-31"
    ),
    array(
        "name" =&gt; "foo",
        "data" =&gt; "2015-01-01",
        "meta" =&gt; array(
            "value" =&gt; 5,
            "something" =&gt; "hello world"
        )
    )
);

$merged = array_merge_defaults($defaults, $custom, 'name');

print_r($merged);


Here's how my final array looks

Array
(
    [0] =&gt; Array
        (
            [name] =&gt; foo
            [level] =&gt; 5
            [meta] =&gt; Array
                (
                    [value] =&gt; 5
                    [something] =&gt; hello world
                )

            [data] =&gt; 2015-01-01
        )

    [1] =&gt; Array
        (
            [name] =&gt; bar
            [level] =&gt; 6
        )

    [2] =&gt; Array
        (
            [name] =&gt; qux
            [date] =&gt; 2016-01-01
        )

    [3] =&gt; Array
        (
            [name] =&gt; tux
            [date] =&gt; 2016-01-31
        )

)
There is an alternative to your implementation. If you can change the position of the name element to the array key:

$defaults = array(
    'foo' =&gt; array(
        "level" =&gt; 5,
        "meta" =&gt; array(
            "value" =&gt; 1
        )
    ),
    'bar' =&gt; array(
        "level" =&gt; 6
    )
);

$custom = array(
    'qux' =&gt; array(
        "date" =&gt; "2016-01-01"
    ),
    'tux' =&gt; array(
        "date" =&gt; "2016-01-31"
    ),
    'foo' =&gt; array(
        "date" =&gt; "2015-01-01",
        "meta" =&gt; array(
            "value" =&gt; 5,
            "something" =&gt; "hello world"
        )
    )
);


You can utilize the native PHP function: array_replace_recursive

Using this function will provide you with the same resulting structure as you have shown with the exception of having the names as array keys. If this is an issue to you, you can duplicate the name once more into an array element. 

The reason this specific/unique key is required is to only merge elements related to each other.

This little snippet should translate your array into the required structure:

/**
 * Translates an array element into an unique key for the element.
 *
 * (The name is just an example)
 */
function uniqify(array $array, string $key): array
{
    foreach($array as $index =&gt; $value)
    {
        $array[$value[$key]] = $value;
        unset($array[$index]);
    }
    return $array;
}


You could then so something like this:

$merged = array_replace_recursive(
    uniqify($defaults, 'name'),
    uniqify($custom,   'name')
);


Hope this can help, happy coding!