Problem in output value of a Object array nested inside a json

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

I know this is old, but in case anyone else could be helped by this.

<?php



$url = 'https://ice3.com/api/v1/orderbook/ticker';

$contents = file_get_contents( $url );

$target = 'BTC/ZAR';


if( !$contents ){
	echo 'Failed to retrieve Contents';
}else{
	$json = json_decode( $contents, true);
	
	$entities = $json['response']['entities'];
	
	
	foreach( $entities as $num=>$entity){
		if( $entity['pair_name'] == $target){
			$info = $entity;
			break;
		}
	}
	
	echo 'Id: ' . $info['pair_id'];
	echo '<br>';
	echo 'Name: ' . $info['pair_name'];
	echo '<br>';
	foreach( ['ask','bid'] as $prop){
		foreach(['price','amount'] as $sProp){
			echo ucwords($prop) . ' ' . ucwords($sProp) . ': ' . $info[$prop][$sProp] . '<br>';	
		}
	}
	
}

thanks to GiTLEZ
please is there any way to make this simpler and short ?

It’s possible to make GitLEZ’s code shorter, but it wouldn’t be as robust; you need to do a lot of checks when getting content from a source you don’t control. There are two price values in the data you’ve given; a bid price and an ask price. Which one are you after?

Yeah, I was just trying to show the simplest of ways. A starting point.

@skawid sorry for late reply… I got blown over it. But now i only have to get back on it. Yes i would prefer it to be robotic enough. I needed to output/echo both price values bid() and ask().

Sponsor our Newsletter | Privacy Policy | Terms of Service