Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php move keys from array into another array

Tags:

arrays

php

key

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?

like image 480
joster222 Avatar asked Sep 27 '26 19:09

joster222


1 Answers

Assuming your arrays are $array1 and $array2:

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

Documentation:

  • array_keys()
  • array_combine()

Online demo

like image 111
Amal Murali Avatar answered Sep 29 '26 09:09

Amal Murali



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!