BigJobbies
BigJobbies

Reputation: 4055

array_merge in PHP not overriding values

I'm having some strange issue with array_merge.

The 2 arrays I have are as follows;

$original = array(
    'details' => array('error' => 0)
    'maxWidth' => 700,
    'maxHeight' => 700,
    'size' => 'original'
);

$defaults = array(
    'details' => array(),
    'maxWidth' => 1024,
    'maxHeight' => 1024,
    'size' => 'original'
);

But when i do

$merge = array_merge($defaults, $original);

It doesn't replace the maxWidth and maxHeight with the updated values of 1024, it keeps them at 700

Any idea how i can fix this up?

Upvotes: 0

Views: 951

Answers (1)

Marvin
Marvin

Reputation: 14415

From the documentation (emphasis by me):

If the input arrays have the same string keys, then the later value for that key will overwrite the previous one.

So currently you start with the values of $defaults and override with $original. Simply switch your arrays:

$merge = array_merge($original, $defaults);

Upvotes: 1

Related Questions