开发者

Accessing specific JSON value

开发者 https://www.devze.com 2023-04-11 01:47 出处:网络
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]

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]开发者_StackOverflow中文版, [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


Have you tried using http://www.php.net/json_decode to turn your JSON into an array and then access the elements as you would a normal array?


$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>";
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号