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

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
                    )
    
            )
    
    )
    
    已采纳该答案
    打赏 评论
  • dongyulan6251 2018-04-13 10:09

    Try array_merge():

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

    OR

    $array[1001][] = ['name'=>'Joe2','surname'=> 'Doe2','age' => 22] + $B;
    
    打赏 评论
  • douzha8489 2018-04-13 10:10
    $innerArray = array();
    $item1Array = array();
    array_push($item1Array,"Data 1");
    array_push($item1Array,"Data 2");
    $item2Array = array();
    array_push($item2Array,"Data 3");
    array_push($item2Array,"Data 4");
    array_push($innerArray, $item1Array);
    array_push($innerArray, $item2Array);
    $result = array();
    $result["result"] = "Success";
    $result["user"] = $innerArray;
    

    Result :

    {
        "result" => "Success",
        "user" => [
            [0] =>[
                [0]=>"Data 1",
                [1]=>"Data 1",
            ],
            [1] =>[
                [0]=>"Data 3",
                [1]=>"Data 4",
            ]
        ]
    }
    

    Hope it helps.

    打赏 评论
  • dptn69182 2018-04-13 10:11

    You can use array_merge like this:

    $array[1001][] = array_merge([
        'name'   => 'Joe2',
        'surname'=> 'Doe2',
        'age' => 22
    ], $B);
    
    打赏 评论

相关推荐 更多相似问题