Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: How to parse a stdClass Object? [closed]

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!

like image 653
Edward Avatar asked May 05 '13 00:05

Edward


1 Answers

$result[0]->delivery_line_1;

$result is an array containing an object.

like image 69
adeneo Avatar answered Sep 22 '22 10:09

adeneo