将元素添加到现有数组php

时间:2014-02-28 09:57:39

标签: php arrays

我有一个如下所示的数组:

Array(
[0] => Array
    (
        [id] => 39
        [countNew] => 0
        [nume] => Ardeleanu
    ) )

如何在现有数组中添加新字段,使其如下所示:

Array(
[0] => Array
    (
        [id] => 39
        [countNew] => 0
        [nume] => Ardeleanu
        [migr] => 0
    ) )

我尝试了阵列推送,但我得到了类似的东西:

Array(
[0] => Array
    (
        [id] => 39
        [countNew] => 0
        [nume] => Ardeleanu
        [0] => Array
            (
                [migr] => 0
            )

    ) )

1 个答案:

答案 0 :(得分:4)

Justr尝试:

$input = array(/* your data */);
$input[0]['migr'] = 0;

或者:

$input = array(/* your data */);
$additional = array('migr' => 0);

$input[0] = array_merge($input[0], $additional);