I have two array with same key but diff value
example :
(
"2"=> 6,
"5"=> 1
),
(
"2"=> 1,
"5"=> 3
)
I want to create new array with the same key but replace highest value
like this
(
"2"=> 6,
"5"=> 3
)
what should I do
2
Answers
Use a nested loop, check if the key is encountered for the first time or if the stored value for that key is less than the current value.
Code: (Demo)
You need to iterate getting the maximum value for each key, which you can do with a combination of
array_keys
,array_combine
,array_map
andarray_column
:Output:
Demo on 3v4l.org