2017-10-17 86 views
-2

在PHP中需要關於json_decode()的幫助和建議。我正在處理API調用的CURL操作,返回結果是JSON。我需要訪問字段pgid的語法是什麼?在PHP上需要幫助json_decode()

捲曲操作

$output = curl_exec($ch); 
$rslt = json_decode($output, true); 
var_dump($rslt); 

var_dump()輸出

array (size=1) 
'protection-groups' => array (size=3) 
    0 => array (size=2) 
     'ppsDropped' => float 0 
     'pgid' => int 13 
    1 => array (size=2) 
     'ppsDropped' => float 7.9957930115316 
     'pgid' => int 18 
    2 => array (size=2) 
     'ppsDropped' => float 5302.7606884163 
     'pgid' => int 19 
+1

你試過的顯示代碼 – Nawin

回答

0

試試下面的代碼,將循環並獲得所有pgid

foreach($rslt['protection-groups'] as $group) 
{ 
    echo $group['pgid']; 
} 
0

請試試這個:

$output = curl_exec($ch); 
$rslt = json_decode($output, true); 
$idsArray = array(); 

foreach($rslt['protection-groups'] as $key => $value){ 
    $pgId = $value['pgid']; 
    echo $pgId; 
    //Or in case you need all the PGID's in array then : 
    array_push($idsArray,$value['pgid']); 
} 

print_r($idsArray); 
exit(); 
+0

它解決了我的問題!謝謝大家的建議。 – TonyT72

+0

高興地幫助你!請接受我的回答。 –