I know there are many other questions on this but my case is quite peculiar, my illegal offset only occurs when I iterate it with the others but is fine when I do it on its own. I am using Slimframework. My array looks like this:
{
"cart": {
"cartId": "9c7b7c3e-d4d3-4de1-afa4-f81e63b50906",
"orderNo": 1,
"orderType": "Collection",
"customerName": "",
"customerTel": "",
"address": "",
"items": [
{
"itemId": 2,
"itemName": "Item A",
"itemPrice": 5.75,
"qty": 1
},
{
"itemId": 1,
"itemName": "Item B",
"itemPrice": 5.25,
"qty": 1
},
{
"itemId": 4,
"itemName": "Item C",
"itemPrice": 9.3,
"qty": 1
},
{
"itemId": 3,
"itemName": "Item D",
"itemPrice": 8.6,
"qty": 1
}
]
},
"shopId": 1,
"discount": 0,
"total": "28.90",
"method": "Card"
}
When I do this in slimframework:
$order = $request->getParsedBody();
$this->response->withJson($order['cart']['items']);
It returns the exact same array moreover, if I check what is $order['cart']['items'][0]['itemPrice']
I get 5.75
.
But when I iterate over it like:
foreach($order['cart']['items'] as $item)
{
$itemId = $item['itemId'];
$item = $item['itemName'];
$itemPrice = $item["itemPrice"];
$itemQty = $item["qty"];
}
I always get Illegal string offset 'itemPrice' & 'qty'.
I have also tried the below and it's not a problem:
$itemPrice;
foreach($order['cart']['items'] as $item)
{
$itemPrice .= $item["itemPrice"]
}
$this->response->withJson($itemPrice);
I might be missing something really obvious, any pointers would be highly appreciated.