简体   繁体   中英

Accessing specific JSON value

I currently have a variable $return which contains JSON-encoded data, which prints out as:

({0:{data:[[0, null], [1, null], [2, null], [3, null], [4, null], [5, null], [6, null], [7, null], [8, null], [9, null], [10, null], [11, null], [12, null], [13, null], [14, null], [15, null], [16, null], [17, null], [18, null], [19, null], [20, null], [21, null], [22, null], [23, null]], label:null, count:null}, 

and so on (too much to copy and paste). Basically what I want to do is find out whether the value of data:[0,null] is null, and then create an if else statement dependant on the result. If it is null, I need to show a message saying "No data available", but if it contains a value I need the value to be displayed.

Could someone explain how I'd access that specific value please?

Thanks for any help

您是否尝试过使用http://www.php.net/json_decode将JSON转换为数组,然后像访问普通数组一样访问元素?

$return=json_decode($return);
if($return['data'][0]){
// what to do if null
}else{
// what to do if not (alternatively use elseif())
}

there is a function called json_decode(). it will simply return you original array of json string..

$ary = array(); 
$ary = json_decode($json_string);
echo "<pre>";print_r($ary);echo "</pre>";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM