Q:

PHP: Merge Multidimensional Arrays Replacing Values

I am trying to merge the following two arrays together based on day_id. If data is present in second array with the same day_id as in array 1, replace it.
Array of days starting with the current day:
Array (
 [0] => Array (
    [day] => Tuesday
    [open] => 06:00:00
    [close] => 15:00:00
    [day_id] => 3
    )
 [1] => Array (
    [day] => Wednesday
    [open] => 06:00:00
    [close] => 15:00:00
    [day_id] => 4
    )
 [2] => Array (
    [day] => Thursday
    [open] => 06:00:00
    [close] => 15:00:00
    [day_id] => 5
    )
 [3] => Array (
    [day] => Friday
    [open] => 06:00:00
    [close] => 15:00:00
    [day_id] => 6
    )
 [4] => Array (
    [day] => Saturday
    [open] =>
    [close] =>
    [day_id] => 7
    )
 [5] => Array (
    [day] => Sunday
    [open] =>
    [close] =>
    [day_id] => 1
    )
 [6] => Array (
    [day] => Monday
    [open] => 06:00:00
    [close] => 15:00:00
    [day_id] => 2
    )
)

Array data that I would like to merge with previous array.
Array (
 [0] => Array (
    [day] => Saturday
    [open] => 07:00:00
    [close] => 15:00:00
    [day_id] => 7
    )
 [1] => Array (
    [day] => Friday
    [open] => 07:00:00
    [close] => 15:00:00
    [day_id] => 6
    )
)

Result I'm trying to get:
 Array (
     [0] => Array (
        [day] => Tuesday
        [open] => 06:00:00
        [close] => 15:00:00
        [day_id] => 3
        )
     [1] => Array (
        [day] => Wednesday
        [open] => 06:00:00
        [close] => 15:00:00
        [day_id] => 4
        )
     [2] => Array (
        [day] => Thursday
        [open] => 06:00:00
        [close] => 15:00:00
        [day_id] => 5
        )
     [3] => Array (
        [day] => Friday
        [open] => 07:00:00
        [close] => 15:00:00
        [day_id] => 6
        )
     [4] => Array (
        [day] => Saturday
        [open] => 07:00:00
        [close] => 15:00:00
        [day_id] => 7
        )
     [5] => Array (
        [day] => Sunday
        [open] =>
        [close] =>
        [day_id] => 1
        )
     [6] => Array (
        [day] => Monday
        [open] => 06:00:00
        [close] => 15:00:00
        [day_id] => 2
        )
    )

I have tried using array_replace_recursive($storeArray, $overrideArray); however that only merges based on the index number. All data is being pulled from a database using two queries.
Thanks for your help!

A:

Try this:
// $original = array with original values
// $override = array with values to override
// $combined = merge result
$combined = array_map(
    function($day) use ($override) {
        foreach ($override as $newDay) {
            if ($day['day_id'] == $newDay['day_id']) {
                return $newDay;
            }
        }
        return $day;
    }, $original
);

... or this to modify $original directly:
array_walk($original,
    function(&$day) use ($override) {
        foreach ($override as $newDay) {
            if ($day['day_id'] == $newDay['day_id']) {
                $day = $newDay;
            }
        }
    }
);

