joster222
joster222

Reputation: 35

php move keys from array into another array

So I have this array:

[0] => 3
[1] => 9
[2] => 4
[3] => 6
[4] => 69
[5] => 8
[6] => 9
[7] => 12
[8] => 9
[9] => 7

And this one

[Far] => 1
[far] => 3
[away] => 1
[behind] => 1
[the] => 23
[word] => 2
[mountains] => 1
[from] => 3
[countries] => 1
[Vokalia] => 1

I would like that the values of the first array will overwrite the values of the second array without changing the keys of the second array. I have already tried fiddling with the foreach function, but no prevail. So in the end I would like it to look like this:

[Far] => 3
[far] => 9
[away] => 4
[behind] => 6
[the] => 69
[word] => 8
[mountains] => 9
[from] => 12
[countries] => 9
[Vokalia] => 7

does anyone know how to do that? And if yes, can that person give a bit more information how it works in the foreach function?

Upvotes: 3

Views: 917

Answers (1)

Amal Murali
Amal Murali

Reputation: 76656

Assuming your arrays are $array1 and $array2:

$keys = array_keys($array2);
$result = array_combine($keys, $array1);

Documentation:

Online demo

Upvotes: 6

Related Questions