I'm getting "illegal offset type" error for every iteration of this code. Here's the code in case anyone can help:
我得到的“非法偏移类型”错误的每一次迭代这段代码。这里有个密码,以防有人能帮忙:
$s = array();
for($i = 0; $i < 20; $i++){
$source = $xml->entry[$i]->source;
$s[$source] += 1;
}
print_r($s)
Any ideas. Thanks in advance.
任何想法。提前谢谢。
5 个解决方案
#1
104
Illegal offset type errors occur when you attempt to access an array index using an object or an array as the index key.
当您试图使用对象或数组作为索引键访问数组索引时,会出现非法偏移类型错误。
Example:
例子:
$x = new stdClass();
$arr = array();
echo $arr[$x];
//illegal offset type
Your $xml
array contains an object or array at $xml->entry[$i]->source
for some value of $i
, and when you try to use that as an index key for $s
, you get that warning. You'll have to make sure $xml
contains what you want it to and that you're accessing it correctly.
您的$xml数组包含一个对象或数组,其值为$xml->条目[$i]->源,值为$i,当您尝试使用它作为$s的索引键时,您会得到这个警告。您必须确保$xml包含您想要的内容,并且正确地访问它。
#2
13
Use trim($source)
before $s[$source]
.
之前使用修剪(源)美元$ s[美元]来源。
#3
2
check $xml->entry[$i] exists and is an object before trying to get a property of it
检查$xml->条目[$i]是否存在,并且是一个对象,然后再尝试获取它的属性
if(isset($xml->entry[$i]) && is_object($xml->entry[$i])){
$source = $xml->entry[$i]->source;
$s[$source] += 1;
}
or $source might not be a legal array offset but an array, object, resource or possibly null
或者$source可能不是一个合法的数组偏移量,而是一个数组、对象、资源或者可能是null
#4
1
There are probably less than 20 entries in your xml.
在xml中可能少于20个条目。
change the code to this
把代码改成这个
for ($i=0;$i< sizeof($xml->entry); $i++)
...
#5
0
I had a similar problem. As I got a Character from my XML child I had to convert it first to a String (or Integer, if you expect one). The following shows how I solved the problem.
我也有类似的问题。当我从XML子元素中获得一个字符时,我必须首先将它转换为一个字符串(如果您希望是整数)。下面展示了我如何解决这个问题。
foreach($xml->children() as $newInstr){
$iInstrument = new Instrument($newInstr['id'],$newInstr->Naam,$newInstr->Key);
$arrInstruments->offsetSet((String)$iInstrument->getID(), $iInstrument);
}
#1
104
Illegal offset type errors occur when you attempt to access an array index using an object or an array as the index key.
当您试图使用对象或数组作为索引键访问数组索引时,会出现非法偏移类型错误。
Example:
例子:
$x = new stdClass();
$arr = array();
echo $arr[$x];
//illegal offset type
Your $xml
array contains an object or array at $xml->entry[$i]->source
for some value of $i
, and when you try to use that as an index key for $s
, you get that warning. You'll have to make sure $xml
contains what you want it to and that you're accessing it correctly.
您的$xml数组包含一个对象或数组,其值为$xml->条目[$i]->源,值为$i,当您尝试使用它作为$s的索引键时,您会得到这个警告。您必须确保$xml包含您想要的内容,并且正确地访问它。
#2
13
Use trim($source)
before $s[$source]
.
之前使用修剪(源)美元$ s[美元]来源。
#3
2
check $xml->entry[$i] exists and is an object before trying to get a property of it
检查$xml->条目[$i]是否存在,并且是一个对象,然后再尝试获取它的属性
if(isset($xml->entry[$i]) && is_object($xml->entry[$i])){
$source = $xml->entry[$i]->source;
$s[$source] += 1;
}
or $source might not be a legal array offset but an array, object, resource or possibly null
或者$source可能不是一个合法的数组偏移量,而是一个数组、对象、资源或者可能是null
#4
1
There are probably less than 20 entries in your xml.
在xml中可能少于20个条目。
change the code to this
把代码改成这个
for ($i=0;$i< sizeof($xml->entry); $i++)
...
#5
0
I had a similar problem. As I got a Character from my XML child I had to convert it first to a String (or Integer, if you expect one). The following shows how I solved the problem.
我也有类似的问题。当我从XML子元素中获得一个字符时,我必须首先将它转换为一个字符串(如果您希望是整数)。下面展示了我如何解决这个问题。
foreach($xml->children() as $newInstr){
$iInstrument = new Instrument($newInstr['id'],$newInstr->Naam,$newInstr->Key);
$arrInstruments->offsetSet((String)$iInstrument->getID(), $iInstrument);
}