donglinxia1541 2016-03-27 13:06 采纳率: 0%
浏览 34
已采纳

PHP:合并2个数组

I've been trying the following for a day now and can't get it to work.

I'm getting information from a paginated source (say 3 pages in this example. So I've got 3 arrays to merge:

Array
(
    [status] => Active
    [nrEntries] => 6
    [entries] => Array
        (
            [0] => Array
                (
                    [sgname] => Merc
                    [timeentered] => 2016-02-08 04:30:00
                )

            [1] => Array
                (
                    [sgname] => bystander
                    [timeentered] => 2016-03-17 20:55:00
                )
        )
)

Array
(
    [status] => Active
    [nrEntries] => 6
    [entries] => Array
        (
            [0] => Array
                (
                    [sgname] => Elvis
                    [timeentered] => 2016-03-08 04:30:00
                )

            [1] => Array
                (
                    [sgname] => marcAR
                    [timeentered] => 2016-03-07 20:55:00
                )
        )
)

Array
(
    [status] => Active
    [nrEntries] => 6
    [entries] => Array
        (
            [0] => Array
                (
                    [sgname] => Killer
                    [timeentered] => 2016-03-09 05:30:00
                )

            [1] => Array
                (
                    [sgname] => MyName
                    [timeentered] => 2016-03-05 21:45:00
                )
        )
)

The result I am looking for is to merge them into 1 array that I can return as a result.

Array
(
    [status] => Active
    [nrEntries] => 6
    [entries] => Array
        (
            [0] => Array
                (
                    [sgname] => Merc
                    [timeentered] => 2016-02-08 04:30:00
                )

            [1] => Array
                (
                    [sgname] => bystander
                    [timeentered] => 2016-03-17 20:55:00
                )

            [2] => Array
                (
                    [sgname] => Elvis
                    [timeentered] => 2016-03-08 04:30:00
                )

            [3] => Array
                (
                    [sgname] => marcAR
                    [timeentered] => 2016-03-07 20:55:00
                )

            [4] => Array
                (
                    [sgname] => Killer
                    [timeentered] => 2016-03-09 05:30:00
                )

            [5] => Array
                (
                    [sgname] => MyName
                    [timeentered] => 2016-03-05 21:45:00
                )
        )
)

The problem that I'm running into is that with array_merge it won't work because of the identical index numbers of the records.

I tried the following, but that doesn't work either.

<?PHP
            // add child array to the end of $result
            for ($i=0 ; $i<2; $i++) {
                $result['entries'][($page*2)+$i][] = $resultChild['entries'][$i];
            }
?>
  • 写回答

3条回答 默认 最新

  • douduonang3169 2016-03-27 13:33
    关注
    $a1['entries'] = array_merge_recursive($a1['entries'], $a2['entries'], $a3['entries']);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答