2011-10-05 52 views
1

我现在有一个变量$返回包含JSON编码的数据,打印出的:访问特定的JSON值

({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}, 

等(太多复制和粘贴)。基本上我想要做的是找出数据的值是否为[0,null]为空,然后根据结果创建一个if else语句。如果它为空,我需要显示一条消息“无可用数据”,但如果它包含一个值,我需要显示该值。

有人可以解释我会如何访问特定的值吗?

感谢所有帮助

回答

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

您是否尝试过使用http://www.php.net/json_decode把你的JSON到一个数组,然后访问的元素,你会正常的阵列?

+0

+1,json_decode是关键的一步 – SW4

0

有一个叫做json_decode()的函数。 它只会返回您JSON字符串的原始数组..

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