dppn67180 2012-07-03 13:55
浏览 147
已采纳

将array_merge_recursive与数字键一起使用

I've been searching through SO and come across a couple solutions that all feel like hacks for getting around the default array_merge_recursive behavior on numeric keys. For instance, I've read that you can add an underscore to the beginning, changing your number to a string.

Anyway, here's my data set and intended outcome...

array ( "Name1", "Name2", "Name3" );
array ( "Data1", "Data2", "Data3" );
array ( "Price1", "Price2", "Price3" );

Intended outcome:

array ( 1 => array ( "Name1", "Data1", "Price1" ), 2 => array ( "Name2", "Data2", "Price2" ), 3 => array ( "Name3", "Data3", "Price3" );

I'm sure you're aware of how array_merge_recursive normally operates with numeric keys... Here's my current merged results.

array ( "Name1", "Name2", "Name3", "Data1", "Data2", "Data3", "Price1", "Price2", "Price3");

Is there a proper method for this? What are the pros and cons to individual methods, like adding an underscore to create a string key?

  • 写回答

2条回答 默认 最新

  • dongmacuo1193 2012-07-03 13:58
    关注

    A simple workaround would be a completely different approach, like:

    $merged = array_map(function () { return func_get_args(); }, $array1, $array2, $array3);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题