【问题标题】:Need help on PHP json_decode() [duplicate]需要有关 PHP json_decode() 的帮助 [重复]
【发布时间】:2017-10-17 04:55:32
【问题描述】:

在 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

【问题讨论】:

  • 你试过的显示代码

标签: php


【解决方案1】:

试试这样:

$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();

【讨论】:

  • 它解决了我的问题!谢谢大家的建议。
  • 很高兴为您提供帮助!请接受我的回答。
【解决方案2】:

尝试下面的代码将循环并获取所有pgid

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-04
    • 2014-12-28
    • 1970-01-01
    • 1970-01-01
    • 2011-11-12
    • 2017-09-20
    • 2020-12-12
    相关资源
    最近更新 更多