Access JSON object name in PHP

I would imagine that the simplest thing to do is to decode into associative arrays instead of stdClass objects

$obj2 = json_decode( $json, true );

foreach ( $obj2['streets'] as $coords => $street )
{   
  echo $coords;
}

Considering this piece of code :

$json = '{"nickname":"xadoc","level":4,"loc":"Tulsa, OK, USA","score":122597,"money":29412.5,"streetNum":8,"streets":{"-91607259\/387798111":{"name":"Alam\u00e9da Ant\u00f3nio S\u00e9rgio","value":243,"type":1},"-91016823\/388182402":{"name":"Autoestrada do Norte","value":18304,"type":1},"-86897820\/399032795":{"name":"Autoestrada do Norte","value":12673,"type":1},"-973092846\/479475465":{"name":"19th Ave","value":7794,"type":1},"-974473223\/480054888":{"name":"23rd Ave NE","value":33977,"type":1}}}';
$obj2 = json_decode($json);
var_dump($obj2);

You'll get :

object(stdClass)[1]
  public 'nickname' => string 'xadoc' (length=5)
  public 'level' => int 4
  public 'loc' => string 'Tulsa, OK, USA' (length=14)
  public 'score' => int 122597
  public 'money' => float 29412.5
  public 'streetNum' => int 8
  public 'streets' => 
    object(stdClass)[2]
      public '-91607259/387798111' => 
        object(stdClass)[3]
          public 'name' => string 'Alaméda António Sérgio' (length=25)
          public 'value' => int 243
          public 'type' => int 1
      public '-91016823/388182402' => 
        object(stdClass)[4]
          public 'name' => string 'Autoestrada do Norte' (length=20)
          public 'value' => int 18304
          public 'type' => int 1
      public '-86897820/399032795' => 
        object(stdClass)[5]
          public 'name' => string 'Autoestrada do Norte' (length=20)
          public 'value' => int 12673
          public 'type' => int 1
      public '-973092846/479475465' => 
        object(stdClass)[6]
          public 'name' => string '19th Ave' (length=8)
          public 'value' => int 7794
          public 'type' => int 1
      public '-974473223/480054888' => 
        object(stdClass)[7]
          public 'name' => string '23rd Ave NE' (length=11)
          public 'value' => int 33977
          public 'type' => int 1

Which means you can loop over the streets like this :

foreach ( $obj2->streets as $id => $street ) {
    echo $id;
    var_dump($street);
    echo '<hr />';
}

With this, for each $street, you'll get the corresponding key into $id -- and the data into $street.


Or you can directly access one this way :

$street = $obj2->streets->{'-86897820/399032795'};
var_dump($street);

Which will get you :

object(stdClass)[5]
  public 'name' => string 'Autoestrada do Norte' (length=20)
  public 'value' => int 12673
  public 'type' => int 1


Your $obj2->street is an object, which means you cannot use array-syntax access ; this explains the "Fatal error: Cannot use object of type stdClass as array" if you try to use this :

$obj2->streets['-86897820/399032795'];

But the properties of your object have quite "strange" names ; which means you cannot do this :

$obj2->streets->-86897820/399032795;

Which gives Parse error: syntax error, unexpected '-', expecting T_STRING or T_VARIABLE or '{' or '$'

Nor that :

$obj2->streets->'-86897820/399032795';

Which also gives Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting T_STRING or T_VARIABLE or '{' or '$'

Happily, you can use {} to kind of "protect" the name of your keys, and get everything working ;-)
(I cannot find the page in the manual that explains this syntax nor give its name... If anyone know... )

Tags:

Php

Json