Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何访问stdClass对象的成员/元素?_Php_Json - Fatal编程技术网

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

Php 如何访问stdClass对象的成员/元素?,php,json,Php,Json,我需要访问stdClass对象的成员/元素,我的响应是json_decode,我需要从结果中访问一些特定元素,这里是我的响应 stdClass Object ( [status] => 200 [result] => Array ( [0] => stdClass Object ( [postcode] => AB10 1XG

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

                )
现在我需要一个变量中的邮政编码

您可以使用

$variable->status
通过访问一个数组的结果,您可以循环它或只是

$array->result[0]->postcode
或者你可以

json_decode($json, true);

然后它返回一个数组。

您可以通过此方法实现

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

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

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

同时也感谢所有支持我的朋友们

您可以使用->操作符访问对象的任何成员,在提出问题之前,您确实应该阅读手册:),但它不会返回任何您使用过的json_decode();你的成绩如何?这将是您需要的变量$array->result是一个数组$array->result[0]->postcodeit是json_decode($json,true);非json_decode($json,true);