简体   繁体   English

如何访问stdClass对象的成员/元素?

[英]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对象的成员/元素,我的响应在json_decode中,我需要从结果中访问某些特定元素,这是我的响应

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 现在我需要一个变量中的邮政编码

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. 它返回一个数组。

你可以做到这一点。

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

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 ! 也感谢所有朋友的支持!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM