'Getting data from array randomly generated number from api

so, i'm recently testing out some apis and i've come to an unexpected array that ive seen.

So, what i'm trying to do is fetch data back from the api, but the api is something i haven't seen before.

The api returns LIST: { their generated id { and the data i need to get here

Looks something like this

Here's the image

So that list { 15, the 15 can be for ex, 20 or 50 based of their ID, so i need to get the data after their generated ID.

So, is there any way i could possibly get the $decode['list'][the id number here]['id']; or other data?

Please let me know.

php


Solution 1:[1]

Since the id/key is dynamic you need to loop it in order to access the data.

// example object data
$list = '{
  "list": {
    "15": {
      "id": "15",
      "ip": "0:0:0:0",
      "host": "0.0.0.0",
      "port": "1234"
    },
    "16": {
      "id": "16",
      "ip": "0:0:0:0",
      "host": "0.0.0.0",
      "port": "1234"
    }
  }
}';

$decoded = json_decode($list, true);

foreach ($decoded['list'] as $key => $value) {
  echo $key . "-" . $value['id'] . " : " . $value['ip'] . "\n";
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 VLDCNDN