我越来越
非法抵消类型
此代码的每次迭代都会出错.这是代码:
$s = array();
for($i = 0; $i < 20; $i++){
$source = $xml->entry[$i]->source;
$s[$source] += 1;
}
print_r($s)
Run Code Online (Sandbox Code Playgroud)
zom*_*bat 137
当您尝试使用对象或数组作为索引键访问数组索引时,会发生非法的偏移类型错误.
例:
$x = new stdClass();
$arr = array();
echo $arr[$x];
//illegal offset type
Run Code Online (Sandbox Code Playgroud)
您的$xml
数组包含一个对象或数组$xml->entry[$i]->source
的某个值$i
,当您尝试将其用作索引键时$s
,您会得到该警告.您必须确保$xml
包含您想要的内容以及您正确访问它.
小智 23
trim($source)
之前 使用$s[$source]
.
在尝试获取它的属性之前检查 $xml->entry[$i] 是否存在并且是否是一个对象
if(isset($xml->entry[$i]) && is_object($xml->entry[$i])){
$source = $xml->entry[$i]->source;
$s[$source] += 1;
}
Run Code Online (Sandbox Code Playgroud)
或 $source 可能不是合法的数组偏移量,而是数组、对象、资源或可能为 null