php PHP非法偏移类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7732109/
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
PHP Illegal offset type
提问by VACIndustries
Warning: Illegal offset type in /email_HANDLER.php on line 85
$final_message = str_replace($from, $to, $final_message);
preg_match_all('/<img[^>]+>/i',$final_message, $result);
$img = array();
foreach($result as $img_tag)
{
preg_match_all("/(alt|title|src)=('[^']*')/i",(string)$img_tag, $img[$img_tag]); //LINE 85
}
Anyone? I'm about to tear my hair out over this...
任何人?我正要为此撕掉我的头发......
here is my var_dump of $img_tag
这是我的 $img_tag 的 var_dump
array(1) {
[0]=>
string(97) "<img alt='' src='http://pete1.netsos.com/site/files/newsletter/banner.jpg' align='' border='0px'>"
回答by Michael Berkowski
Assuming $img_tag
is an object of some type, rather than a proper string, cast $img_tag
to a string inside the []
假设$img_tag
是某种类型的对象,而不是正确的字符串,$img_tag
转换为[]
preg_match_all("/(alt|title|src)=('[^']*')/i",(string)$img_tag, $img[(string)$img_tag]);
//------------------------------------------------------------------^^^^^^^^^
Some object types, notably SimpleXMLElement
for example, will return a string representation to print/echo
via the magic method __toString()
, but cannot stand in as regular strings. Attempts to use them as array keys will yield the illegal offset type
error unless you cast them to proper strings via (string)$obj
.
一些对象类型,特别SimpleXMLElement
是例如,将print/echo
通过魔法方法__toString()
返回一个字符串表示,但不能作为常规字符串。尝试将它们用作数组键会产生illegal offset type
错误,除非您通过(string)$obj
.
回答by eykanal
See first comment on this PHP bug report:
请参阅有关此 PHP 错误报告的第一条评论:
You cannot use arrays or objects as keys. Doing so will result in a warning: Illegal offset type. Check your code.
不能使用数组或对象作为键。这样做会导致警告:非法偏移类型。检查您的代码。
Ensure that $img_tag
is of the appropriate variable type.
确保它$img_tag
是适当的变量类型。
回答by RiaD
$result
is 2-dimentional array.So, $img_tag
should be an array.
$result
是二维$img_tag
数组。所以,应该是数组。
But only integers and strings may be used as offset
但只能使用整数和字符串作为偏移量
回答by NizNiz
foreach( $result[0] as $img_tag)
it works
有用