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

时间:2015-07-09 06:12:02

标签: php json

我需要访问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
                        )

                )

现在我需要变量

中的邮政编码

3 个答案:

答案 0 :(得分:1)

您可以使用

访问它
$variable->status

访问结果是一个数组,你可以循环它或只是

$array->result[0]->postcode

或者你可以

json_decode($json, true);

并返回一个数组。

答案 1 :(得分:1)

你可以通过这个来实现。

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

答案 2 :(得分:0)

我找到了一个解决方案,首先我需要将其转换为数组,然后我可以轻松获取任何成员或元素,

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

还要感谢所有朋友的支持!