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

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

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?

图片转代码服务由CSDN问答提供 功能建议

这是我的代码:

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

这里是当前输出:

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

这里是预期的结果:

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

因为中的发送数组( $ arr2 ) code> array_merge()包含 p 键,所以我想把它放在最后。 知道我该怎么办?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

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.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题