3
3
33balabol2016-05-07 20:34:02
PHP
33balabol, 2016-05-07 20:34:02

how to get array from api?

Upon request, the Api displays the following:
{ "error": "false", "response": [{ "id": 1, "game": "ETS2", "ip": "37.187.170.151", "port": 42860, "name": "Europe 1", "shortname": "EU #1", "online": true, "players": 738, "maxplayers": 2300, "speedlimiter": 1 }, { "id" : 3, "game": "ETS2", "ip": "191.101.3.39", "port": 42860, "name": "United States", "shortname": "US 1", "online": true , "players": 9, "maxplayers": 1000, "speedlimiter": 1 }, { "id": 4, "game": "ETS2", "ip": "37.187.170.151", "port": 42880, "name": "Europe 2", "shortname": "EU 2", "online": true, "players": 1445, "maxplayers": 2300, " speedlimiter": 0 }]}
How to get separate values ​​where "id": 1, "id": 3 and so on

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Yarkov, 2016-05-08
@33balabol

<?php

$str = '{  
    "error":"false",
    "response":[  
      {  
        "id":1,
        "game":"ETS2",
        "ip":"37.187.170.151",
        "port":42860,
        "name":"Europe 1",
        "shortname":"EU #1",
        "online":true,
        "players":738,
        "maxplayers":2300,
        "speedlimiter":1
      },
      {  
        "id":3,
        "game":"ETS2",
        "ip":"191.101.3.39",
        "port":42860,
        "name":"United States",
        "shortname":"US 1",
        "online":true,
        "players":9,
        "maxplayers":1000,
        "speedlimiter":1
      },
      {  
        "id":4,
        "game":"ETS2",
        "ip":"37.187.170.151",
        "port":42880,
        "name":"Europe 2",
        "shortname":"EU 2",
        "online":true,
        "players":1445,
        "maxplayers":2300,
        "speedlimiter":0
      }
    ]
}';

$json = json_decode($str, true);

echo $json['response'][0]['id']."<br>";
echo $json['response'][1]['id'];

?>

R
Rou1997, 2016-05-07
@Rou1997

Like this:
$json = json_decode($str, true);
echo $json['response'][0]['id'];
echo $json['response'][1]['id'];

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question