duanpao4522 2014-02-19 14:44
浏览 232
已采纳

php将数组中的键移动到另一个数组中

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?

  • 写回答

1条回答 默认 最新

  • douzuqin3467 2014-02-19 14:47
    关注

    Assuming your arrays are $array1 and $array2:

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

    Documentation:

    Online demo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大