Search code examples
phparrayslaravelarray-push

How to solve this php associative array push


I have an array like below:

$storedItem = ['item_id' => $ItemInfo['ItemId'], 'qty' => [], 'price' => $item->price, 'size' => [], 'produ' => [], 'item' => $item];

I have 2 string variables called $size and $quantity.

I need to push them into produ array. Then I used

> array_push($storedItem['produ'], $size);

But, the thing is I need to push $size and $quantity several times into produ array. But, when I use array_push again & again, it overwrite the array.

But, what I want like below.

$storedItem['produ'] = [];
$storedItem['produ'][0] => 0 => $size, 1=>$quantity
$storedItem['produ'][1] => 0 => $size, 1=>$quantity
$storedItem['produ'][2] => 0 => $size, 1=>$quantity

How can I do like this?


Solution

  • Maybe this solve your problem:

    $storedItem['produ'][] = [$size, $quantity];