简体   繁体   English

尝试访问数组时尝试获取非对象的属性

[英]Trying to get property of non-object when trying to access array

I'm trying to access a array via its index, but I need to get this from the query string. 我正在尝试通过其索引访问数组,但是我需要从查询字符串中获取它。

I've tried: 我试过了:

 $var = $_GET['id'];
 echo $xml->subway->line[$var]->name;

That doesn't work. 那不行 But this does: 但这确实是:

 echo $xml->subway->line[0]->name;

try this: 尝试这个:

$var = isset($_GET['id']) ? (int)$_GET['id'] : false;

if ($var !== false && isset($xml->subway->line[$var]) {
   echo $xml->subway->line[$var]->name;
} else {
   echo 'Your problem is either "id" not being in $_GET or $xml object does not have line with that index';

   var_dump($_GET); //see what is 'id'
}

Everytime you are getting item from an array by index you should check if it is set first to avoid errors like this. 每次从索引数组中获取项目时,都应检查是否首先设置了该项目,以避免出现此类错误。

It's likely that your $_GET variable is coming back as a string instead of an integer so PHP will be assuming the index doesn't exist. 您的$_GET变量很可能以string而不是integer因此PHP将假定索引不存在。 Try type casting it as 尝试将其强制转换为

$var = (int)$_GET['id'];

Which forces PHP to treat the $_GET as an integer 这迫使PHP将$_GET视为integer

暂无
暂无

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

相关问题 获取:“尝试获取非对象的属性”时尝试访问对象数组中的值 - Getting: ' Trying to get property of non-object' when trying to access the value in an array of objects 试图在视图返回中访问数组试图获取非对象的属性 - Trying to access to array in view return Trying to get property of non-object 尝试在视图上访问Auth :: user() - > name时尝试获取非对象[Laravel]的属性 - Trying to get property of non-object [Laravel] when trying to access Auth::user()->name on view 对象数组:想访问一个属性,但是我得到“试图在…中获取非对象的属性” - array of objects: want to access one property but I get “Trying to get property of non-object in …” PHP-错误:尝试为购物车数组元素分配变量时,“试图获取非对象的属性” - PHP - error: 'Trying to get property of non-object' when trying to assign variables with shopping cart array elements 尝试检索数组时出错“消息:尝试获取非对象的属性” - Error “Message: Trying to get property of non-object” when trying to retrieve an array 在对象上使用时尝试获取非对象的属性 - Trying to get property of non-object when used on object 在变量中获取值时尝试获取非对象的属性 - Trying to get property of non-object when get value in variable 消息:尝试获取ID时获取非对象的属性 - Message: Trying to get property of non-object when get ID 试图获取非对象的属性,但这是一个对象 - Trying to get property of non-object, but it is an object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM