1

I've 2 arrays:

{"1":"red"}
{"1":"green","2":"red"}

It should remove 1 = green and replace it with 1 = red. Key 2 must simply remain.

So, I want an array like this:

{"1":"red","2":"red"}

How can I do that in PHP?

2
  • 1
    What did you try? Commented Feb 24, 2018 at 8:38
  • It's already fixed. The array 1 + array 2 fixed it ;) Commented Feb 26, 2018 at 18:54

1 Answer 1

1

You could use the operator + :

$a = [1 => "red"] ;
$b = [1 => "green", 2 => "red"] ;
print_r($a + $b) ;

Outputs :

Array
(
    [1] => red
    [2] => red
)

From documentation :
The + operator returns the right-hand array appended to the left-hand array; for keys that exist in both arrays, the elements from the left-hand array will be used, and the matching elements from the right-hand array will be ignored.

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.