PHP在数组中添加数组

时间:2018-04-13 10:04:52

标签: php arrays

我有问题将数组添加到数组中。我无法解决它。

我有这个数组$ A:

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

我有这个数组$ B:

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

如何创建新数组以获得此结果:

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

我用它,但结果不正确:

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

感谢您的回答。

4 个答案:

答案 0 :(得分:4)

您可以使用运算符+合并数组:

$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);

输出:

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
                )

        )

)

答案 1 :(得分:3)

尝试使用array_merge():

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

OR

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

答案 2 :(得分:2)

您可以像这样使用array_merge

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

答案 3 :(得分:0)

$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" => "Success",
    "user" => [
        [0] =>[
            [0]=>"Data 1",
            [1]=>"Data 1",
        ],
        [1] =>[
            [0]=>"Data 3",
            [1]=>"Data 4",
        ]
    ]
}

希望它有所帮助。