; this is what i've tried? for display : ADA/ETH bestAsk":0.000699,"bestBid":0.000607 ?
$url = "https://exchange-api.lcx.com/market/tickers";
$json = json_decode(file_get_contents($url), true);
$data = $json["data"];
$bestAsk = $json['bestAsk'];
$bestBid = $json['bestBid'];
echo $data;
echo $bestAsk;
echo $bestBid;
how to get data?
; any ideea?
This should get you started:
$url = "https://exchange-api.lcx.com/market/tickers";
$json = json_decode(file_get_contents($url), true);
$data = $json["data"];
foreach($data as $key => $value ){
#var_dump($value);
echo $key." - BESTBID:".$value["bestBid"]."\n";
echo $key." - BESTASK:".$value["bestAsk"]."\n";
}
output:
ADA/ETH - BESTBID:0.000607
ADA/ETH - BESTASK:0.000699
ADA/EUR - BESTBID:1.6681
ADA/EUR - BESTASK:2.2
ADA/LCX - BESTBID:28.1101
ADA/LCX - BESTASK:45
AMP/EUR - BESTBID:0.044
AMP/EUR - BESTASK:0.067839
AMP/LCX - BESTBID:0.85
AMP/LCX - BESTASK:1
....
Your code is working perfectly in my system and the data fetched successfully.
if you want to display JSON in PHP you can use print_r statement instead of echo:
print_r($data);
Your should try this one!
$url = "https://exchange-api.lcx.com/market/tickers";
$json = json_decode(file_get_contents($url), true);
$data = $json["data"];
foreach($data as $key => $value ){
#var_dump($value);
echo $key." - BESTBID:".$value["bestBid"]."\n";
echo $key." - BESTASK:".$value["bestAsk"]."\n";
}
Related
{
USD_INR: {
2017-12-31: 63.830002
},
INR_USD: {
2017-12-31: 0.015667
}
}
I have tried with following method,
$url = file_get_contents("https://free.currencyconverterapi.com/api/v5/convert?q=USD_INR,INR_USD&compact=ultra&date=2017-12-31");
$result = json_decode($url);
echo $result->USD_INR;
but it did't works, can any one help me where I'm doing wrong.
Just pass date in $todaysDate variable which you want price.
$todaysDate = "2017-12-31";
$url = file_get_contents("https://free.currencyconverterapi.com/api/v5/convert?q=USD_INR,INR_USD&compact=ultra&date=$todaysDate");
$result = json_decode($url);
echo ($result->USD_INR->$todaysDate);
You can add true as a second parameter on json_decode to make it associative array.
$url = file_get_contents("https://free.currencyconverterapi.com/api/v5/convert?q=USD_INR,INR_USD&compact=ultra&date=2017-12-31");
$result = json_decode($url, true);
And access it as:
echo $result["USD_INR"]["2017-12-31"];
This will result to: 63.830002
Doc: http://php.net/manual/en/function.json-decode.php
You can foreach loop to get all the values
$url = file_get_contents("https://free.currencyconverterapi.com/api/v5/convert?q=USD_INR,INR_USD&compact=ultra&date=2017-12-31");
$result = json_decode($url, true);
foreach ( $result as $value ) {
foreach ( $value as $date => $rate ) {
echo $date . ": " . $rate . "<br />";
}
}
This will result to:
2017-12-31: 63.830002
2017-12-31: 0.015667
You are trying to echo an object either use print_r like this to print USD_INR
$url = '{"USD_INR":{"2017-12-31":63.830002},"INR_USD":{"2017-12-31":0.015667}}';
$result = json_decode($url);
print_r($result->USD_INR);
OR to echo value of 2017-12-31 inside USD_INR
$url = '{"USD_INR":{"2017-12-31":63.830002},"INR_USD":{"2017-12-31":0.015667}}';
$result = json_decode($url);
echo $result->USD_INR->{'2017-12-31'};
Using {'2017-12-31'} because 2017-12-31 is not proper variable name
Live demo
how can I get the same results/values in one loop rather than two?
$json = file_get_contents($url)
$data = json_decode($json, true);
$desc = $data["descriptions"];
$assets = $data["assets"];
foreach ($assets as $assItem) {
echo $assItem["assetid"];
}
foreach($desc as $descItem) {
echo descItem["name"];
}
I've tried something like
$json = file_get_contents($url);
$data = json_decode($json, true);
foreach ($data as $item) {
echo $item["assets"]["assetid"];
echo $item["descriptions"]["name"];
}
pastebin to the json: https://pastebin.com/raw/uA9mvE2e
You could do something like:
$json = file_get_contents($url);
$data = json_decode($json, true);
foreach ($data['assets'] as $k => $item) {
echo $item["assetid"];
echo $data["descriptions"][$k]["name"];
}
This assumes that $data['assets'] and $data['descriptions'] share the same indices.
I want to echo out each element of an object of a JSON array like this:
{"request_list":[{"id":"1","name":"yunus","surname":"smsk","phone_number":"05350601922","blood_type":"0","unit_of_blood":"0","date":null},{"id":"3","name":"yunus","surname":"smsk","phone_number":"05350601922","blood_type":"0","unit_of_blood":"0","date":null}]}
But i cannot do it.I tried somethinglikethis:
$object = json_decode($json, true);
$request_list = $object->request_list;
foreach($request_list as $r){
echo $r->name;
echo $r->blood_type;
echo $r->phone_number;
}
But I got an error like:
Invalid argument supplied for foreach()
As you have mark return as array true in json_decode. So, try below code.
$object = json_decode($json, true);
$request_list = $object['request_list'];
foreach($request_list as $r){
echo $r['name'];
echo $r['blood_type'];
echo $r['phone_number'];
}
Use this
$object = json_decode($json, true);
$request_list = $object['request_list'];
foreach($request_list as $r){
echo $r['name'];
echo $r['blood_type'];
echo $r['phone_number'];
}
try
$json = '{"request_list":[{"id":"1","name":"yunus","surname":"smsk","phone_number":"05350601922","blood_type":"0","unit_of_blood":"0","date":null},{"id":"3","name":"yunus","surname":"smsk","phone_number":"05350601922","blood_type":"0","unit_of_blood":"0","date":null}]}';
$data = json_decode($json );
$request = $data->request_list;
foreach($request as $request_data){
echo $request_data->id;
echo $request_data->name;
echo $request_data->surname;
}
i have the following JSON:
{"Switches":["Auswahl1","Auswahl2","Auswahl3"],"Check_MK":["Auswahl1","Auswahl2","Auswahl3"],"Testgroup":["Auswahl1","Auswahl2","Auswahl3"],"Printer":["Auswahl1","Auswahl2","Auswahl3"],"CAD":["Auswahl1","Auswahl2","Auswahl3"]}
How do i loop each object while using PHP?
My thoughts were the following:
<?php
$jsonfile = file_get_contents('tags.json');
echo $jsonfile . "<br><br>";
$decode = json_decode($jsonfile);
foreach($decode as $key => $value) {
echo $key . $value;
}
?>
Doesn't work..... Also
echo $decode[1];
and
echo $decode[1][1];
doesn't work..
You need to add a second parameter to json_decode()
This parameter returns associative array instead of existing object (if existing).
$decode = json_decode($jsonfile, TRUE);
This will convert your JSON decoded data into associative array.
$jsonfile = file_get_contents('tags.json');
echo $jsonfile . "<br><br>";
$decode = json_decode($jsonfile);
now $decode is equivalent to:
$decode = new stdClass();
$decode->Switches = array();
$decode->Switches[] = "Auswahl1";
$decode->Switches[] = "Auswahl2";
$decode->Switches[] = "Auswahl3";
$decode->Check_MK = array();
...
I have a json file that I want to read with PHP
http://rh.ernestek.cz.cc/webeast/static.txt
I wanted to extract the "id" and the "key"
I wanted to loop through the id and key so that I can produce a table like this:
ID Key
1 9d07d681e0c1e294264724f7726e6f6f29
3 9cd1e3a4a04b5208862c3140046f73b515
...
I tried to extract the first id and key out using the following code but no luck:
<?php
$json = file_get_contents('static.txt');
$json_decoded = json_decode($json);
echo "ID: ".$json_decoded->static->1->id."<br />";
echo "Key: ".$json_decoded->static->1->key."<br />";
?>
Is there anything I an wrong?
Any ideas?
Thanks!
Decode the json as an array (pass true as second parameter), and loop over the array like so
<?php
$json = file_get_contents('static.txt');
$json_decoded = json_decode($json, true);
foreach ($json_decoded['static'] as $item) {
echo 'ID: ', $item['id'], '<br/>';
echo 'Key: ', $item['key'], '<br/>';
}
By using true in json_decode() it will convert all objects to array:
$json = file_get_contents("http://rh.ernestek.cz.cc/webeast/static.txt");
$json_decoded = json_decode($json, true); // return array
$table = array();
foreach($json_decoded["static"] as $array){
$table[$array["id"]] = $array["key"];
}
//creating the table for output
$output = '<table border="1"><tr><td>ID</td><td>Key</td></tr>';
foreach($table as $id => $key){
$output .= "<tr><td>$id</td><td>$key</td></tr>";
}
$output .= '</table>';
echo $output;