如何解决这个 php 关联数组推送
How to solve this php associative array push
我有一个如下所示的数组:
$storedItem = ['item_id' => $ItemInfo['ItemId'], 'qty' => [], 'price' => $item->price, 'size' => [], 'produ' => [], 'item' => $item];
我有 2 个字符串变量,名为 $size
和 $quantity
。
我需要将它们推入 produ 数组。然后我用了
> array_push($storedItem['produ'], $size);
但是,问题是我需要将 $size
和 $quantity
多次推入 produ 数组。但是,当我一次又一次地使用 array_push
时,它会覆盖数组。
但是,我想要的如下。
$storedItem['produ'] = [];
$storedItem['produ'][0] => 0 => $size, 1=>$quantity
$storedItem['produ'][1] => 0 => $size, 1=>$quantity
$storedItem['produ'][2] => 0 => $size, 1=>$quantity
我该怎么做?
也许这可以解决您的问题:
$storedItem['produ'][] = [$size, $quantity];
我有一个如下所示的数组:
$storedItem = ['item_id' => $ItemInfo['ItemId'], 'qty' => [], 'price' => $item->price, 'size' => [], 'produ' => [], 'item' => $item];
我有 2 个字符串变量,名为 $size
和 $quantity
。
我需要将它们推入 produ 数组。然后我用了
> array_push($storedItem['produ'], $size);
但是,问题是我需要将 $size
和 $quantity
多次推入 produ 数组。但是,当我一次又一次地使用 array_push
时,它会覆盖数组。
但是,我想要的如下。
$storedItem['produ'] = [];
$storedItem['produ'][0] => 0 => $size, 1=>$quantity
$storedItem['produ'][1] => 0 => $size, 1=>$quantity
$storedItem['produ'][2] => 0 => $size, 1=>$quantity
我该怎么做?
也许这可以解决您的问题:
$storedItem['produ'][] = [$size, $quantity];