Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

hello everyone. im working on a js

Status
Not open for further replies.

charlion

Programmer
Jun 30, 2017
3
ZA
hello everyone.
im working on a json from this site. im trying to get the "last" key and
im trying to echo my own premiunm exchange rate.by getting 10% of the last key and add it up to the last key
my codes was like this
<code>
<?php
$url = " $fileGet = file_get_contents($url);
$json = json_decode($fileGet, TRUE);
$json = $json["ticker"]["last"];
$premium = 10; //in percentage
$premium = $premium / 100;
$dollar_amount = $premium * $json;
$customer_price = $json + $dollar_amount;
echo $customer_price;
?>
</code>
there was no error indicated.but it just printed "0" on the screen.
please help. i dont know where something does wrong.

but everything work fine to line 4 $json=$json["ticker"]["last"]; because it print the correct last ticker on the browser.
 
There is no "ticker" index in that json array.

So you should be getting an Undefined Index Notice because there no addressable item there. But your PHP may be set to suppress notices.

To see exactly what the array looks like and be able to address it correctly, its always a good idea to print it out. You can use print_r() to do so.

Code:
$json = json_decode($fileGet, TRUE);
echo "<pre>" . print_r($json,1) . "</pre>";

This should print out:

[pre]
Array
(
[btc_usd] => Array
(
[high] => 2486.96
[low] => 2379.112
[avg] => 2433.036
[vol] => 8576408.53547
[vol_cur] => 3526.96822
[last] => 2414.001
[buy] => 2415.99
[sell] => 2414.001
[updated] => 1498927868
)

)
[/pre]

So as feherke points out you should be addressing $json['btc_usd']['last']

----------------------------------
Phil AKA Vacunita
----------------------------------
OS-ception: Running Linux on a Virtual Machine in Windows which itself is running in a Virtual Machine on Mac OSx.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top