I get data from an API and I display it on a page in a table. The structure of the data set is as follows:
"products": [
{
"id": 0,
"title": "string",
"sku": "string",
"barcode": "string",
"product_number": "string",
"price": 0,
"stock": [
{
"warehouse": {
"warehouse_id": 0,
"name": "string"
},
"quantity": 0
}
],
"status": "string",
}
]
and the code to display the records is follows:
foreach ($items as $key => $row) {
echo
"<tr>
<td class='centerCol'>" . $counter++ . "</td>
<td class='productTitle'>" . $row['title'] . "</td>
<td class='centerCol'>" . "$" . $row['price'] . "</td>
<td class='centerCol'>" . $row['status'] . "</td>
<td class='centerCol'>" . $row['stock'] . "</td>
</tr>";
}
The stock field has an inner array so I want to know where I display it
<td class='centerCol'>" . $row['stock'] . "</td>
can I loop through the inner array right there where I display it or is there a way to use a while loop that will stop at the stock row and loop through that first before moving onto the next record in the main array? Any guidance will be greatly appreciated.
Here is a dump of the data:
array (
'warehouse' =>
array (
'warehouse_id' => 1,
'name' => 'CPT',
),
'quantity_available' => 5,
)
array (
'warehouse' =>
array (
'warehouse_id' => 3,
'name' => 'JHB',
),
'quantity_available' => 21,
)
On the page there are two columns one is for JHB and one is for CPT and so I need to display the numer for CPT in the CPT column and the number for JHB in the JHB column