【问题标题】:Using PHP's foreach to create array of values from JSON data使用 PHP 的 foreach 从 JSON 数据创建值数组
【发布时间】:2011-05-09 22:13:25
【问题描述】:

我是第一次使用 JSON 数据,我有一些 PHP 来获取一些 JSON 数据,如下所示(除了 body 中有数百个 measuregrps >)。

$json = file_get_contents("http://wbsapi.withings.net/measure?action=getmeasures");
$json_o = json_decode($json);

例如,我如何使用 foreach 为 type = 1 创建一个一维数组?

    {
        "status": 0,
        "body": {
            "updatetime": 1249409679,
            "measuregrps": [
                {
                    "grpid": 2909,
                    "attrib": 0,
                    "date": 1222930968,
                    "category": 1,
                    "measures": [
                        {
                            "value": 79300,
                            "type": 1,
                            "unit": -3
                        },
                        {
                            "value": 652,
                            "type": 5,
                            "unit": -1
                        },
                        {
                            "value": 178,
                            "type": 6,
                            "unit": -1
                        },
                        {
                            "value": 14125,
                            "type": 8,
                            "unit": -3
                        }
                    ]
                },
                {
                    "grpid": 2908,
                    "attrib": 0,
                    "date": 1222930968,
                    "category": 1,
                    "measures": [
                        {
                            "value": 78010,
                            "type": 1,
                            "unit": -3
                        }
                    ]
                },
                {
                    "grpid": 2907,
                    "attrib": 0,
                    "date": 1222930968,
                    "category": 1,
                    "measures": [
                        {
                            "value": 77300,
                            "type": 1,
                            "unit": -3
                        },
                        {
                            "value": 678,
                            "type": 5,
                            "unit": -1
                        }

                    ]
                },


            ]
        }
    }

【问题讨论】:

  • 有时候,提问的人比阅读的人更清楚自己的目标。确切地说,输出应该是什么? array(/* ... */) 符号会特别有用。

标签: php json foreach


【解决方案1】:
$json_o = json_decode($json,true);

$result = array();

foreach ($json_o['body']['measuregrps'] as $measuregrp)
 foreach ($measuregrp['measures'] as $measure)
  if ($measure['type'] == 1)
   $result []= $measure['value'];

【讨论】:

  • 但是下次请尝试自己编写一些代码,如果它不起作用,我们可以提供帮助。
  • 这实际上是行不通的,因为body 等是stdClass 的实例。它们不是数组,因此不能称为数组。
  • 它确实有效,因为第二个参数表示结果应该使用关联数组而不是标准对象。 php.net/manual/en/function.json-decode.php
  • 我讨厌 PHP 中对象和数组的令人发指的混合,所以我总是这样称呼 JSON decode。
【解决方案2】:

类似

$values = array();

foreach($json_o->body->measuregrps as $group){
  foreach($group->measures as $measure){
    if($measure->type == 1){
      $values[] = $measure->value;
    }
  }
}

print_r($values);

会的

【讨论】:

    猜你喜欢
    • 2013-08-20
    • 1970-01-01
    • 1970-01-01
    • 2012-11-29
    • 2021-10-25
    • 2012-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多