PHP: How to parse a stdClass Object?
Asked Answered
D

1

7

I'm trying to parse this data that is returned from SmartyStreets (it's an address verification company).

Here is an example:

Array
(
    [0] => stdClass Object
        (
            [input_index] => 0
            [candidate_index] => 0
            [delivery_line_1] => 3785 Las Vegas Blvd S
            [last_line] => Las Vegas NV 89109-4333
            [delivery_point_barcode] => 891094333992
            [components] => stdClass Object
                (
                    [primary_number] => 3785
                    [street_name] => Las Vegas
                    [street_postdirection] => S
                    [street_suffix] => Blvd
                    [city_name] => Las Vegas
                    [state_abbreviation] => NV
                    [zipcode] => 89109
                    [plus4_code] => 4333
                    [delivery_point] => 99
                    [delivery_point_check_digit] => 2
                )

            [metadata] => stdClass Object
                (
                    [record_type] => H
                    [county_fips] => 32003
                    [county_name] => Clark
                    [carrier_route] => C024
                    [congressional_district] => 01
                    [building_default_indicator] => Y
                    [rdi] => Commercial
                    [latitude] => 36.10357
                    [longitude] => -115.17295
                    [precision] => Zip9
                )

            [analysis] => stdClass Object
                (
                    [dpv_match_code] => D
                    [dpv_footnotes] => AAN1
                    [dpv_cmra] => N
                    [dpv_vacant] => N
                    [active] => Y
                    [footnotes] => B#H#L#M#
                )

        )

)

I'm new to working in Objects, so I'm having difficulty understanding how this works. From the above print_r $result output, I'm trying to access [delivery_line1], [city_name], [state_abbreviation], [zipcode] and [plus4_code].

How do I access this tree in PHP?

I've tried this:

echo $result["delivery_line_1"];

But that gives "Undefined index: delivery_line_1".

echo $result->delivery_line_1;

But that gives "Trying to get property of non-object".

What am I missing? Thanks!

Deluca answered 5/5, 2013 at 0:12 Comment(3)
Okay, is this a string you’re trying to parse, or is this an array from which you’re attempting to extract a property?Cerous
This is not "parsing". This is called "traversal" or simply accessing variables.Schmidt
if you dont like to mess with objects, you can alway call json_decode(json_encode($arr),TRUE) and have a nice assoc array, with the cost 2 additional calls.Dynamo
M
10
$result[0]->delivery_line_1;

$result is an array containing an object.

Moonshot answered 5/5, 2013 at 0:13 Comment(2)
Thanks! Please tell me, what is [0]? Is it the entire Object or part of this Object? For example, if there were more data returned in this query would there be a [1] stdClass Object?Deluca
You have an array with numbered indices, like 0, 1, 2.. etc, each array value is an object, so first you select the array value with a [0] key (or whatever the key is), and then you have the object, where you would use the arrow to access a certain property.Moonshot

© 2022 - 2024 — McMap. All rights reserved.