F0l0w
F0l0w

Reputation: 261

php using file_get_contents with an array

I am trying to pull values from the following url: https://api.binance.com/api/v1/ticker/allPrices

What I want to accomplish: Obtain all the "symbol" values printed in the website (one under the other)

The result I am looking for, would be:

ETHBTC
LTCBTC
...

This is what I have done so far:

<?php
$url = "https://api.binance.com/api/v1/ticker/allPrices";
$content = file_get_contents($url);
$json_data = json_decode($content, true);

for ($i=0; $i < count($json_data); $i++) { 
    # code...
    print_r($json_data);
}
?>

The result it brings is the entire data (the symbols, prices, plus other irrelevant data).

Thanks in advance.

Upvotes: 0

Views: 3620

Answers (3)

Goma
Goma

Reputation: 1981

You can use foreach loop, then echo the value by symbol key

$url = "https://api.binance.com/api/v1/ticker/allPrices";
$content = file_get_contents($url);
$json_data = json_decode($content, true);

foreach ($json_data as $value) {
    echo $value["symbol"] . '<br>';
}

Upvotes: 2

Chris Applegate
Chris Applegate

Reputation: 762

This ought to do it:

<?php
$url = "https://api.binance.com/api/v1/ticker/allPrices";
$content = file_get_contents($url);
$json_data = json_decode($content, true);

for ($i=0; $i < count($json_data); $i++) {
    echo $json_data[$i]['symbol'] . "\n";
}
?>

Upvotes: 2

Ctznkane525
Ctznkane525

Reputation: 7465

json_data["symbol"] 

instead in the print

Upvotes: 0

Related Questions