2

I'm working with JSON data for the first time and I've got some PHP to grab some JSON data like below (except there are hundreds of measuregrps within body).

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

How do I use foreach to, say, create a 1-dimensional array of values for 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
                        }

                    ]
                },


            ]
        }
    }
1
  • Sometimes one's goal is clearer to the person asking than the person reading. What, exactly, should the output be? array(/* ... */) notation would be especially helpful. Commented Nov 14, 2010 at 20:01

2 Answers 2

2
$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'];
Sign up to request clarification or add additional context in comments.

4 Comments

But next time please try to come up with some code yourself, then we can help if it doesn't work.
This actually won't work, as body etc. are instances of stdClass. They are not arrays, so they cannot be called as arrays.
It does work, since the second parameter says that the result should use associative arrays instead of standard objects. php.net/manual/en/function.json-decode.php
I hate heinous mixtures of objects and arrays in PHP, so I always call JSON decode like that.
0

Something like

$values = array();

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

print_r($values);

will do

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.