dpde7365 2018-04-20 20:27
浏览 143
已采纳

如何将第二个数组放在合并数组的末尾?

Here is my code:

$arr1 = ['p' => 2, 'pm' => 1];
$arr2 = ['p' => ''];
print_r(array_merge($arr1, $arr2));

And here is the current output:

Array
(
    [p] => 
    [pm] => 1
)

And here the expected result:

Array
(
    [pm] => 1
    [p] => 
)

Because the send array ($arr2) in array_merge() contains p key, so I want to put it in the end. Any idea how can I do that?

  • 写回答

1条回答 默认 最新

  • dpxw17759 2018-04-20 20:34
    关注

    Probably not the best solution, but I would remove the entries first and then add them again:

    print_r(array_merge(array_diff_key($arr1, $arr2), $arr2));
    

    Docs for array_diff_key.

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

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?