dongnanke4106 2012-04-16 19:19
浏览 29
已采纳

php - array_combine或array_merge?

I'm sure this is easy for someone well-versed in php, but I've made the mistake of overloading my brain, so now I'm really confused as to whether I should use array_combine, array_merge, or something else... I've been googling and reading php.net for 4 hours and I think I'm just confusing myself even more...

Essentially, I just want to combine an array while keeping the keys?

//Here are the original arrays
[field_sreference] => Array
    (
        [0] => Array
            (
                [nid] => 28
            )

        [1] => Array
            (
                [nid] => 28
            )

        [2] => Array
            (
                [nid] => 29
            )
    )

[field_idelta] => Array
    (
        [0] => Array
            (
                [value] => 0
            )

        [1] => Array
            (
                [value] => 1
            )

        [2] => Array
            (
                [value] => 0
            )
    )

[field_iswitch] => Array
    (
        [0] => Array
            (
                [value] => 0
            )

        [1] => Array
            (
                [value] => 0
            )

        [2] => Array
            (
                [value] => 0
            )
    )

//Here is what I'm trying to achieve:

[combinedarray] => Array
        (
            [0] => Array
                (
                    [nid] => 28
                    [idelta] => 0
                    [iswitch] => 0

                )

            [1] => Array
                (
                    [nid] => 28
                    [idelta] => 1
                    [iswitch] => 0
                )

            [2] => Array
                (
                    [nid] => 29
                    [idelta] => 0
                    [iswitch] => 0
                )
        )
  • 写回答

2条回答 默认 最新

  • doulin8374 2012-04-16 19:26
    关注

    You can solve this is O(n) by simply iterating the arrays...

    $combinedarray = array();
    $len = count($field_sreference);
    for ($i = 0; $i < $len; $i++) {
        $combinedarray[] = array("nid" => $field_sreference[$i]['nid'], 
                                 "idelta" => $filed_idelta[$i]['value'], 
                                 "iswitch" => $field_iswitch[$i]['value']);
    }
    

    This assumes, the 3 arrays are all of equal length.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建