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

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条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!