我越来越
非法胶印类型
此代码每次迭代的错误。这是代码:
$s = array();
for($i = 0; $i < 20; $i++){
$source = $xml->entry[$i]->source;
$s[$source] += 1;
}
print_r($s)
Answers:
当您尝试使用对象或数组作为索引键访问数组索引时,会发生非法偏移类型错误。
例:
$x = new stdClass();
$arr = array();
echo $arr[$x];
//illegal offset type
您的$xml
数组包含一个对象或数组,其$xml->entry[$i]->source
值为的某个值$i
,当您尝试将其用作的索引键时$s
,会收到警告。您必须确保$xml
包含所需的内容并正确访问它。
$xml
使用某种XML解析器创建了变量?simple_xml还是DOMDocument?在这种情况下,源节点实际上可能是某种dom元素对象。
$xml->entry[$i]->source->div
。如果要将HTML解析为DOM结构,则DomDocument
具有一个loadHTML()
处理HTML的功能比SimpleXML更好的功能。查看php.net/manual/en/domdocument.loadhtml.php
$source
是的一个实例,SimpleXML
并提供仅适用于该特定情况的信息。虽然最终是这样,但问题并未说明,无论谁来这里进行参考,都应考虑到这一点。