I have been trying to add a new value pair to an array called products.
This is my array before the loop:
"products": [
{
"id": "050435",
"name": "Test Product",
"price": 10,
}
]
This is my loop:
$item = [];
foreach ($products as $product) {
$item['new_item'] = 'item_value';
}
$products[] = $item;
Now I need to do this as to obtain the 'item_value' I will need to work with the data inside this array to get this value which I can do later. However I get this as a result when I am trying to add this item to this array.
"products": [
{
"id": "050435",
"name": "Test Product",
"price": 10,
},
{
"new_item": "item_value"
}
]
I have tried array_merge and trying $products[0][] etc but I can't get this inside the products array. Any help will be great thanks. This is how I want it to be:
"products": [
{
"id": "050435",
"name": "Test Product",
"price": 10,
"new_item": "item_value"
},
]