php将键从数组移动到另一个数组

时间:2022-10-12 10:44:16

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:

我希望第一个数组的值将覆盖第二个数组的值,而不更改第二个数组的键。我已经尝试过修改foreach函数,但是没有成功。最后我希望它是这样的:

[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?

有人知道怎么做吗?如果是,那个人能提供更多关于foreach函数的信息吗?

1 个解决方案

#1


5  

Assuming your arrays are $array1 and $array2:

假设数组为$array1和$array2:

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

Documentation:

文档:

Online demo

在线演示

#1


5  

Assuming your arrays are $array1 and $array2:

假设数组为$array1和$array2:

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

Documentation:

文档:

Online demo

在线演示