php 注意:尝试获取非对象错误的属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22636826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Notice: Trying to get property of non-object error
提问by Ale? Kalan
i am trying to get data from:
我正在尝试从以下位置获取数据:
http://api.convoytrucking.net/api.php?api_key=public&show=player&player_name=Mick_Gibson
http://api.convoytrucking.net/api.php?api_key=public&show=player&player_name=Mick_Gibson
but if i want to get player_name variable with this code:
但是如果我想使用以下代码获取 player_name 变量:
<?
$js = file_get_contents('http://api.convoytrucking.net/api.php?api_key=public&show=player&player_name=Mick_Gibson');
$pjs = json_decode($js);
var_dump($pjs->{'player_name'});
?>
i get error:
我得到错误:
Notice: Trying to get property of non-object in **\htdocs\index.php on line 9 + var_dump() returns: NULL
注意:尝试在第 9 行的 **\htdocs\index.php 中获取非对象的属性 + var_dump() 返回:NULL
var_dump($pjs)
returns:
var_dump($pjs)
返回:
array(1) { [0]=> object(stdClass)#52 (15) { ["player_name"]=> string(11) "Mick_Gibson" ["player_id"]=> int(88) ["rank"]=> string(12) "FIRE TURTLEE" ["lastseen"]=> int(1393797692) ["registration_date"]=> string(19) "2012-08-10 17:01:34" ["last_mission_date"]=> string(19) "2014-03-02 21:41:50" ["time_offset"]=> int(1) ["house_id"]=> int(611) ["fines"]=> int(0) ["wanted"]=> int(0) ["police_badge"]=> bool(true) ["vip"]=> bool(false) ["staff"]=> NULL ["stats"]=> object(stdClass)#53 (23) { ["score"]=> int(2941) ["convoy_score"]=> int(818) ["ARTIC"]=> int(515) ["DUMPER"]=> int(565) ["TANKER"]=> int(56) ["CEMENT"]=> int(163) ["TRASH"]=> int(7) ["ARMORED"]=> int(9) ["VAN"]=> int(501) ["TOW"]=> int(502) ["COACH"]=> int(4) ["LIMO"]=> int(97) ["ARRESTS"]=> int(272) ["GTA"]=> int(67) ["BURGLAR"]=> int(122) ["HEIST"]=> int(1) ["PLANE"]=> int(48) ["HELI"]=> int(12) ["FAILED"]=> int(312) ["OVERLOADS"]=> int(160) ["TRUCK_LOADS"]=> int(1275) ["ODOMETER"]=> int(28320798) ["TIME"]=> int(2078450) } ["achievements"]=> array(4) { [0]=> string(20) "Professional Trucker" [1]=> string(13) "Gravel Hauler" [2]=> string(12) "Delivery Boy" [3]=> string(7) "Wrecker" } } }
回答by Jakub Matczak
This is because $pjs
is an one-element-array of objects, so first you should access the array element, which is an object and then access its attributes.
这是因为$pjs
是一个对象的单元素数组,所以首先你应该访问数组元素,它是一个对象,然后访问它的属性。
echo $pjs[0]->player_name;
Actually dump result that you pasted tells it very clearly.
实际上,您粘贴的转储结果非常清楚地说明了这一点。
回答by Sahil Mittal
The response is an array.
响应是一个数组。
var_dump($pjs[0]->{'player_name'});
回答by Sahil Mittal
@Balamanigandan your Original Post :- PHP Notice: Trying to get property of non-object error
@Balamanigandan 您的原始帖子:- PHP 通知:尝试获取非对象错误的属性
Your are trying to access the Null Object. From AngularJS your are not passing any Objects instead you are passing the $_GET element. Try by using $_GET['uid']
instead of $objData->token
您正在尝试访问空对象。在 AngularJS 中,您没有传递任何对象,而是传递了 $_GET 元素。尝试使用$_GET['uid']
代替$objData->token