2011-10-05 79 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>";