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 关于#python#的问题:求帮写python代码
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?