Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php,数组_push,添加到现有数组_Php_Arrays - Fatal编程技术网

Php,数组_push,添加到现有数组

Php,数组_push,添加到现有数组,php,arrays,Php,Arrays,我正在尝试使用array_push()向现有多维数组添加额外的条目 这是我的阵列: $array= Array ( [0] => Array ( [label] => Black [quantity] => 10 ) [1] => Array ( [label] => Yellow [quantity] =

我正在尝试使用array_push()向现有多维数组添加额外的条目

这是我的阵列: $array=

Array
(
    [0] => Array
        (
            [label] => Black
            [quantity] => 10
        )

    [1] => Array
        (
            [label] => Yellow
            [quantity] => 20
        )

    [2] => Array
        (
            [label] => Red
            [quantity] => 30
        )
)
我现在需要的是在每个[数量]之后添加价格键,因此最终结果是:

Array
(
    [0] => Array
        (
            [label] => Black
            [quantity] => 10
            [price] => 0
        )

    [1] => Array
        (
            [label] => Yellow
            [quantity] => 20
            [price] => 0
        )

    [2] => Array
        (
            [label] => Red
            [quantity] => 30
            [price] => 0
        )
)
$price['price'][]=0
我尝试过使用
array\u push($price['price',$array)


但这不起作用,它只返回数字2。

您有一个数组。您需要对其进行迭代,以将价格添加到每个子数组中

foreach($array as $key => $value) {
  $array[$key]['price'] = 0;
}

在这种情况下,我认为您不想使用
array\u push()

这样,您就有了2个数组。不要高估第一个。
foreach ($price as $priceItem) {
$priceItem['price']=0;
$newPrice[]= $priceItem;
}
var_dump($newPrice);