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= “”; ////////////////////////////source

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

Here is the structure of the json from the source :

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.


$url = '';

$contents = file_get_contents( $url );

$target = 'BTC/ZAR';

if( !$contents ){
	echo 'Failed to retrieve Contents';
	$json = json_decode( $contents, true);
	$entities = $json['response']['entities'];
	foreach( $entities as $num=>$entity){
		if( $entity['pair_name'] == $target){
			$info = $entity;
	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