For a school assignment I’m trying to split stockitems with product details like colour and size in the title into groups of stockitems with different variants. I’ve got as far as having them all split, but I just can’t figure out how to add this information to the $stockItem array. ($stockItem is inside the array $stockItemGroup which is inside the array $stockItemGroups).
When I try to add information to the array inside the loop, I cannot access that information outside the loop. If I use print_r on the entire array after this loop has completed the new information is not displayed.
for($i = 0; $i < count($stockItemGroup); $i++){ $stockItem = $stockItemGroup[$i]; $restString = str_replace($similarString, "", $stockItem['StockItemName']); $colour = getColour($restString, $allColours); $restVariant = getRestVariant($restString, $allColours); $stockItemGroup[$i]['Colour'] = $colour; $stockItemGroup[$i]['RestVariant'] = $restVariant; $stockItemGroup[$i]['NewItemName'] = createNewItemName($colour, $restVariant, $stockItem['StockItemName']); }
I have tried both in a foreach and a for loop (I read that a foreach does some copying, so I thought that might cause it). but to no avail.
I have also obviously tried
$stockItem['Colour'] = $colour; $stockItem['RestVariant'] = $restVariant; $stockItem['NewItemName'] = createNewItemName($colour, $restVariant, $stockItem['StockItemName']);
But that did not change anything either.
I am a total Php noob, so it might be very obvious, any help would be appreciated.
EDIT: this loop is inside a method which is called in this loop:
$stockItemGroups = getStockItemGroups(); foreach ($stockItemGroups as $stockItemGroup){ addVariants($stockItemGroup); //writeNewGroup($stockItemGroup); }
Advertisement
Answer
foreach ($stockItemGroups as &$stockItemGroup){ Pass the array as a reference – RiggsFolly