在PHP上需要帮助json_decode()

时间:2017-10-17 04:55:32

标签: php

在PHP中需要有关json_decode()的帮助和建议。我正在进行API调用的CURL操作,返回结果是JSON。访问字段pgid需要什么语法?

CURL操作

$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

2 个答案:

答案 0 :(得分:0)

尝试以下代码循环并获取所有pgid

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

答案 1 :(得分: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();