使用PHP';s foreach从JSON数据创建值数组

使用PHP';s foreach从JSON数据创建值数组,php,json,foreach,Php,Json,Foreach,我第一次使用JSON数据,我有一些PHP来获取一些JSON数据,如下所示(除了body中有数百个measuregps) 我如何使用foreach为type=1创建一维值数组 { "status": 0, "body": { "updatetime": 1249409679, "measuregrps": [ { "grpid": 2909

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

我如何使用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
                        }

                    ]
                },


            ]
        }
    }
差不多

$values = array();

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

print_r($values);

会做的

有时候,一个人的目标对于提出要求的人来说比阅读的人更清楚。确切地说,输出应该是什么<代码>数组(/*…*/)符号将特别有用。但下次请尝试自己编写一些代码,如果它不起作用,我们可以提供帮助。这实际上不起作用,因为
body
等都是stdClass的实例。它们不是数组,因此不能称为数组。它确实有效,因为第二个参数表示结果应该使用关联数组而不是标准对象。我讨厌PHP中对象和数组的混合,所以我总是这样调用JSON解码。
$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'];
$values = array();

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

print_r($values);