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

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

报告相同问题?

悬赏问题

  • ¥100 c语言,请帮蒟蒻看一个题
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)