doujiong3146 2018-04-13 10:04
浏览 275
已采纳

PHP在数组中添加数组

I have problem to add array into array. I cannot resolve it.

I have this array $A:

Array
(
    [1001] => Array
            (
                [0] => Array
                    (
                        [name] => 'Joe'
                        [surname] => 'Doe'
                        [age]  => 20
                        [height] => 180
                        [weight] => 80
                     )
            )
)

And I have this array $B:

Array
(
    [height] => 200
    [weight] => 100
)

How to create new array to get this result:

Array
(
    [1001] => Array
            (
                [0] => Array
                    (
                        [name] => 'Joe'
                        [surname] => 'Doe'
                        [height] => 180
                        [weight] => 80
                        [age]  => 20
                     )
            )
    [1001] => Array
            (
                [1] => Array
                    (
                        [name] => 'Joe2'
                        [surname] => 'Doe2'
                        [height] => 200
                        [weight] => 100
                        [age]  => 22
                     )
            )
)

I use this, but the result is not correct:

$array[1001][] = [
            'name'   => 'Joe2',
            'surname'=> 'Doe2',
            $B,
            'age' => 22
        ];

Thank you for answer.

  • 写回答

4条回答 默认 最新

  • dongyan1625 2018-04-13 10:08
    关注

    You can use the operator + to merge your arrays:

    $array = array(
        1001 => array(
            array(
                'name' => 'Joe',
                'surname' => 'Doe',
                'height' => 180,
                'weight' => 80,
                'age'  => 20
            )
        ),
    );
    
    $B = array('height' => 200, 'weight' => 100);
    
    $array[1001][] = [
                'name'   => 'Joe2',
                'surname'=> 'Doe2',
                'age' => 22
            ] + $B;
    
    print_r($array);
    

    Will outputs:

    Array
    (
        [1001] => Array
            (
                [0] => Array
                    (
                        [name] => Joe
                        [surname] => Doe
                        [height] => 180
                        [weight] => 80
                        [age] => 20
                    )
    
                [1] => Array
                    (
                        [name] => Joe2
                        [surname] => Doe2
                        [age] => 22
                        [height] => 200
                        [weight] => 100
                    )
    
            )
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?