2017-10-17 85 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

高兴地帮助你!请接受我的回答。 –