Jump to content

Error working with Object array nested inside a json


Recommended Posts

Hello everyone.

please I have problem with printing out a value from inside an array of a nested json. ive tried several ways its always returns "index not define".
here is the code .

$valr= "https://ice3.com/api/v1/orderbook/ticker";    ////////////////////////////source

	 $valrGet =  file_get_contents($valr);
	 $valrD =  json_decode($valrGet, true);
	 $valrSell = ["ask"] ["price"];
                 

Here is the structure of the json from the source :

{"errors":false,"response":{"entities":[{"pair_id":3,"pair_name":"BTC\/ZAR","ask":{"price":"179382.54","amount":"0.05357142"},"bid":{"price":"177229.4286563","amount":"0.0011"}},{"pair_id":4,"pair_name":"BTC\/NGN","ask":{"price":"8890000.00","amount":"0.10"},"bid": 

my target is to  output  the value of  ("price") from "pair_name"   BTC\/ZAR

please help. Thanks in advance

Link to post
Share on other sites

However, using the string just as far as the the first entity

$valrD = json_decode(valrGet, true);
echo '<pre>$valrD = ', print_r($valrD, 1), '</pre>';

gives

image.png.1ac20b439cd69d639ebb0c4ea885e326.png

therefore

$target = 'BTC/ZAR';

foreach ($valrD['response']['entities'] as $k => $ents) {
    if ($ents['pair_name'] == $target) {
        echo "$target asking price : {$ents['ask']['price']}<br>";
        break;
    }
}

outputs "BTC/ZAR asking price : 179382.54"

 

  • Like 1
  • Great Answer 1
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.