views:

136

answers:

2

I'm pretty new to arrays still. I need some help - I have some JSON, and I've run it through some PHP that basically parses the JSON and decodes it as follows:

stdClass Object
(
    [2010091907] => stdClass Object
        (
        [home] => stdClass Object
            (
                [score] => stdClass Object
                    (
                        [1] => 7
                        [2] => 17
                        [3] => 10
                        [4] => 7
                        [5] => 0
                        [T] => 41
                    )

                [abbr] => ATL
                [to] => 2
            )

This actually goes on and on - BUT - my problem is the stdClass Object part. I need to be able to call this in a for loop and then iterate through each section (home, score, abbr, to, etc). How would I go about this?

+3  A: 

You can use get_object_vars() to get an array of the object's properties, or call json_decode() with json_decode($string,true); to get an associative array.


Example:

<?php
$foo = array('123456' =>
 array('bar' =>
        array('foo'=>1,'bar'=>2)));


//as object
var_dump($opt1 = json_decode(json_encode($foo)));

echo $opt1->{'123456'}->bar->foo;

foreach(get_object_vars($opt1->{'123456'}->bar) as $key => $value){
    echo $key.':'.$value.PHP_EOL;
}

//as array
var_dump($opt2 = json_decode(json_encode($foo),true));

echo $opt2['123456']['bar']['foo'];

foreach($opt2['123456']['bar'] as $key => $value){
    echo $key.':'.$value.PHP_EOL;
}
?>

Output:

object(stdClass)#1 (1) {
  ["123456"]=>
  object(stdClass)#2 (1) {
    ["bar"]=>
    object(stdClass)#3 (2) {
      ["foo"]=>
      int(1)
      ["bar"]=>
      int(2)
    }
  }
}
1
foo:1
bar:2

array(1) {
  [123456]=>
  array(1) {
    ["bar"]=>
    array(2) {
      ["foo"]=>
      int(1)
      ["bar"]=>
      int(2)
    }
  }
}
1
foo:1
bar:2
Wrikken
I have done the `json_decode($string,true);`, however, I have issues called the number referenced for the array. For example, the number is 2010091907 but when I issue `foreach ($json->2010091907 as $game)`, I get the error `Parse error: syntax error, unexpected T_LNUMBER, expecting T_STRING or T_VARIABLE`
drewrockshard
Use `$json->{'2010091907'}` for 'illegal' variable names.
Wrikken
Almost there: `foreach ($json->{'2010091907'} as $game)`, I get the error: `Warning: Invalid argument supplied for foreach()`. reference my output above for the JSON to ARRAY. I need to be able to call the data and these seem to be recursive arrays (not sure what that's officially called).
drewrockshard
Have you just tried the `json_decode($string,true);` route? Much simpler then keep casting them to an array...
Wrikken
I'm not sure I follow - I'm recieving the JSON via a URL. I'm then placing the whole JSON output into an ARRAY (via `json_decode($data,true);`. From there, I'm wanting to iterate through the array contents and call certain data from the array. Could you provide some code, as I might be doing this all wrong.
drewrockshard
@drew: you shouldn't have an object if you pass `true` as the second parameter, unless you have you home-grown implementation of the `json_encode()` function in stead of the built-in one. Added an example which works as a standalone script here.
Wrikken
What if I'm trying to PULL in JSON data? This looks like I'm CREATING JSON data, however, I'm trying to pull in JSON data and then use the values in my code. I don't need to create JSON data. I could be wrong though since this doesn't make sense to me. Here' the code I have so far, which brings in the json code and where I'm stuck at: http://pastie.org/private/we9ey0iq8fhcmrwvuceng
drewrockshard
Makes absolutely _no_ difference, `json` is an established format, I just added the `json_encode()` in there to _have_ a json-encoded string for the example. Your `$json=json_decode($data,true);` should yield an array of arrays, or something very fishy is going on.
Wrikken
I understand this now, for the most part. I was able to get it working - placing it into a database and getting each "header" (which is that string 2010091907, which is random) on its own will require further reading, but for now, at least I know how to manage the key/values for the JSON output. I appriciate your help.
drewrockshard
A: 

You can iterate on the stdClass with foreach.

Yorirou
Damn, you're right. Why didn't the PHP people have the decency to make `stdClass instanceof Traversable` true I wonder....
Wrikken