charlion Posted October 1, 2020 Share Posted October 1, 2020 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 Quote Link to comment https://forums.phpfreaks.com/topic/311544-error-working-with-object-array-nested-inside-a-json/ Share on other sites More sharing options...
Barand Posted October 1, 2020 Share Posted October 1, 2020 Your json has been truncated and therefore invalid. Quote Link to comment https://forums.phpfreaks.com/topic/311544-error-working-with-object-array-nested-inside-a-json/#findComment-1581666 Share on other sites More sharing options...
Barand Posted October 1, 2020 Share Posted October 1, 2020 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 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" 1 1 Quote Link to comment https://forums.phpfreaks.com/topic/311544-error-working-with-object-array-nested-inside-a-json/#findComment-1581674 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.