2

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

  • Assumptions:- that you are getting the same structure every time with the same key and as you mention in the loop you can access the array. foreach ($items as $key => $row) { echo " " . $counter++ . " " . $row['title'] . " " . "$" . $row['price'] . " " . $row['status'] . " " . $row['stock']['quantity'] . " "; } – Bhimani Rutvik Jun 28 '22 at 12:19
  • @BhimaniRutvik `stock` is an array, so no, that won't work. I suggest you also read the duplicate question that we've recommended to the OP above :-) – ADyson Jun 28 '22 at 12:23
  • `can I loop through the inner array right there where I display it`...yes. Just end your string, then start a loop, and append more stuff to the string within the loop. – ADyson Jun 28 '22 at 12:54
  • This is my code foreach ($row['stock'] as $val) { foreach ($val as $num) { print_r($num); } } and I get this back Array ( [warehouse_id] => 1 [name] => CPT ) 5Array ( [warehouse_id] => 3 [name] => JHB ) 21 How do I get to display the number 5 and 21? I mean if it's CPT I need to display 5 and if it's JHB I need to display 21 –  Jun 28 '22 at 13:57
  • Well just echo the quantity field specifically, instead of looping through them all. Should be `$val["quantity"]` I expect. – ADyson Jun 28 '22 at 14:01
  • ok thanks but it shows both quantities I only want to display one. –  Jun 28 '22 at 14:15
  • Maybe more efficient if edit your question to add a dump of $items, please? – Monnomcjo Jun 28 '22 at 14:18
  • `I only want to display one`...well, what is the criteria by which you want to decide which one to display? – ADyson Jun 28 '22 at 14:21
  • I edited my question thank you so much for all this guidance and help –  Jun 28 '22 at 14:42
  • 1
    Thanks. `I need to display the numer for CPT in the CPT column and the number for JHB in the JHB column`...so will there always be exactly 2 entries in the stock array, once for each warehouse? Or can it be more or less entries than that sometimes? – ADyson Jun 28 '22 at 14:48
  • yes correct there are only two warehouses now. Maybe in the future there could be more but not now. –  Jun 28 '22 at 15:09
  • something likes that? https://onlinephp.io/c/e5735 – Monnomcjo Jun 28 '22 at 15:11
  • Stackoverflow should really make links open in a new tab ha ha. Thank you I am going to see if I can make it work using that code. Appreciate the help really –  Jun 28 '22 at 15:14
  • This is where it gets confusing and it makes no sense. If you cannot echo an array then why does my echo statement display the values of the array, but seeing this question is closed I am going to ask another question –  Jun 29 '22 at 03:23
  • @ADyson yes that same problem I think that's why I mention assumption first, thanks for pointing that. :) – Bhimani Rutvik Jun 29 '22 at 05:27
  • @EagerLearner which echo statement do you mean exactly? If you echo the whole array that won't work, but you can loop through the items and echo each one individually, as we've shown – ADyson Jun 29 '22 at 06:16
  • The question is closed because if you follow the guidance in the duplicate and learn the principles you can apply it to your situation and work out what to do. Stackoverflow is an encyclopaedia rather than a helpdesk. Your question is already answered by that guidance. We don't need a separate post showing the same concepts for every piece of JSON in existence :-) – ADyson Jun 29 '22 at 06:17
  • No problem I sorted it out. Apologies for wasting anybody's time –  Jun 29 '22 at 06:34

0 Answers0