Ali Raza
Ali Raza

Reputation: 183

how to access a member/element of an stdClass Object?

I need to access the member/element of an stdClass Object, my response is in json_decode I need to access some specific element from the result here is my response

stdClass Object
(
    [status] => 200
    [result] => Array
        (
            [0] => stdClass Object
                (
                    [postcode] => AB10 1XG
                    [quality] => 1
                    [eastings] => 393149
                    [northings] => 805924
                    [country] => Scotland
                    [nhs_ha] => Grampian
                    [longitude] => -2.1148480012509
                    [latitude] => 57.14416009476
                    [parliamentary_constituency] => Aberdeen South
                    [european_electoral_region] => Scotland
                    [primary_care_trust] => Aberdeen City Community Health   Partnership
                    [region] => 
                    [lsoa] => West End North - 01
                    [msoa] => West End North
                    [nuts] => Gilcomston
                    [incode] => 1XG
                    [outcode] => AB10
                    [distance] => 0.564254268
                    [admin_district] => Aberdeen City
                    [parish] => 
                    [admin_county] => 
                    [admin_ward] => Midstocket/Rosemount
                    [ccg] => Aberdeen City Community Health Partnership
                    [codes] => stdClass Object
                        (
                            [admin_district] => S12000033
                            [admin_county] => S99999999
                            [admin_ward] => S13002482
                            [parish] => S99999999
                            [ccg] => S03000012
                        )

                )

Now I need postcode in a variable

Upvotes: 0

Views: 213

Answers (3)

jmattheis
jmattheis

Reputation: 11125

You can access it with

$variable->status

with accessing result which is an array you can loop it or just

$array->result[0]->postcode

Or you can

json_decode($json, true);

and it returns an array.

Upvotes: 1

Ali Raza
Ali Raza

Reputation: 183

I have found a solution for this, first I need to convert it into array then I can fetch easily any member or element,

$value = get_object_vars($decodedvalues);
$val =  $value['result']['0']['postcode'];

And also Thanks all my friends for your support !

Upvotes: 0

Qaisar Satti
Qaisar Satti

Reputation: 2762

you can achieve by this.

$array->result[0]->postcode;

Upvotes: 1

Related Questions