Merge two arrays, the keys of the first are the values in the second
first array
Array
(
[vegetables] => 2
[fruits] => 4
)
second array
Array
(
[0] => Array
(
[id] => 68
[text] => vegetables
)
[1] => Array
(
[id] => 60
[text] => fruits
)
)
I want to get a combined array like this:
Array
(
[0] => Array
(
[id] => 68
[text] => vegetables
[quantity] => 2
)
[1] => Array
(
[id] => 60
[text] => fruits
[quantity] => 4
)
)
I understand that this needs to be done in a loop, but how? Confused about keys and values. I’m new to PHP
2
Answers
Let us name these arrays $first and $second respectively.
The code below must add a ‘quantity’ property to each iteration of the $second array:
I did not debug the code, but the idea is like that.
You can do this with the code:
example output: