stdClass Object problems
Asked Answered
G

6

5

I'm struggling to parse the below data using PHP. An API returns it, and I've tried various syntaxes. How do I return the data in a non-object way? Or, what's the syntax to call the data using the stdClass?

Could I convert this to one data based array, or even two? I'm lost when it comes to object-based data sets.

stdClass Object
(
    [0] => stdClass Object
        (
            [district] => stdClass Object
                (
                    [state] => NY
                    [number] => 29
                )

        )

    [1] => stdClass Object
        (
            [district] => stdClass Object
                (
                    [state] => NY
                    [number] => 26
                )

        )

)

When i create the object on my own, and then do a var_dump, I get this:

object(stdClass)#8 (2) {
  [0]=>
  object(stdClass)#4 (1) {
    ["district"]=>
    object(stdClass)#5 (2) {
      ["state"]=>
      string(2) "NY"
      ["number"]=>
      string(2) "29"
    }
  }
  [1]=>
  object(stdClass)#6 (1) {
    ["district"]=>
    object(stdClass)#7 (2) {
      ["state"]=>
      string(2) "NY"
      ["number"]=>
      string(2) "26"
    }
  }
}
Greenleaf answered 14/10, 2009 at 17:10 Comment(2)
Where is this object comming from?Mascle
From this API, emmense.com/sunlight-labs-php-library <?php include('class.sunlightlabs.php'); $sf = new SunlightDistrict; $sf->api_key = '[api key]'; echo '<pre>'; print_r( $sf->districtsByZipCode( 14485 ) ); echo '</pre>'; ?>Greenleaf
B
14

They are probably casting arrays to objects in their code ($object = (object) $array). This has the advantage that it will be passed by reference from now on (as is the default with objects) and the disadvantage that the object is completely useless (members cannot start with numbers - see the regex in PHP's docs) until you cast it back (PHP does allow some very mysterious things):

$array = (array) $bogusObject;
$array[0]->district->state === 'NY';
Benyamin answered 14/10, 2009 at 17:42 Comment(2)
Ah ha! I had a similar problem once with someone using XML tags like ad-text - loading it into a SimpleXML object and trying to access $ad->ad-text didn't work.Algophobia
Actually no need to cast to an array ... can use the $object->{0}->district... as well.Kenn
K
10

Use:

$object->{'0'}->district->state

Basically You're short-cutting assigning a string to a variable, then using that variable as your object accessor.

$zero = "0";
$object->$zero; /* or */ $object->{$zero};
Kenn answered 14/10, 2009 at 17:57 Comment(0)
R
4

I'm looking at their code now, and unfortunately, they've not exposed the option in their class for you to request the data as a associative array tree vs a stdClass object tree.

The "problem" is at line 96 in class.sunlightlabs.php

return json_decode( $data );

You have a couple options.

  1. Just use the stdClass syntax.
  2. Convert the returned stdClass tree into a associative array one

#1 in action

// echo the state of the 2nd object in the result
echo $result->{0}->district->state;

#2 in action

$result = toArray( $result );

function toArray( $data )
{
  if ( is_object( $data ) )
  {
    $data = get_object_vars( $data );
  }
  return is_array($data) ? array_map(__FUNCTION__, $data) : $data;
}

You could also work with their class directly via some creative application of patterns, but they make heavy use of sublcasses already which complicates it quite a bit, so I'd stick to one of these two solutions.

Ralston answered 14/10, 2009 at 17:46 Comment(2)
If you can change json_decode( $data ); to json_decode( $data, TRUE ); Then the function will return an array.Kenn
thanks for the get_object_vars suggestion that works, however, the json_decode($data, TRUE) suggestion is the best fix for this issue. thanks!Jocosity
E
1

You can iterate through the object like this:

foreach ($obj as $each) {
    echo $each->district->state . ' - ' . $each->district->number . '<br />';
}
Entwine answered 14/10, 2009 at 17:46 Comment(2)
This works, but isn't exactly what i'm looking for. Thank you, though.Greenleaf
I'm glad it helped. I wasn't trying to exactly match your needs, but rather to help answer your question, "what's the syntax to call the data using the stdClass?" (as well as to demonstrate that the stdClass is iterable).Entwine
J
1

The user 'null' suggested this in the comments, but want to put it here so it's not missed so easly.

The best option is to pass TRUE as the second param in json_decode i.e. json_decode($data, TRUE) which makes it return associative arrays instead of classes. So if you have access to the source code – make that change.

Jocosity answered 23/8, 2010 at 6:20 Comment(0)
U
0

This solution from soulmerge just worked fine for me:

$array = (array) $bogusObject;
$array[0]->district->state === 'NY';

Thanks a lot!!

Urfa answered 24/7, 2010 at 19:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.