-2

Need help and advice on json_decode() in PHP. I am working on a CURL operation for an API call and the return result is a JSON. What is the syntax that I need to access the field pgid?

CURL Operation

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

The var_dump() output

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
Antti29
  • 2,953
  • 12
  • 34
  • 36
TonyT72
  • 31
  • 1

2 Answers2

0

try below code that will loop through and get all pgid

foreach($rslt['protection-groups'] as $group)
{
   echo $group['pgid'];
}
Pankaj Makwana
  • 3,030
  • 6
  • 31
  • 47
usman ikram
  • 461
  • 4
  • 10
0

Try like this :

$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();
Ketan Solanki
  • 697
  • 5
  • 13