SimpleXMLElement和Warning:非法偏移类型

时间:2022-02-22 20:07:13

Warning: Illegal offset type

警告:非法的偏移类型

i have this var from an xml output

我有一个xml输出的变量

var_dump($key);

its give me

它给我

object(SimpleXMLElement)#11 (1) { [0]=> string(5) "Cairo" } 

now i want make "Cairo" as a key to get its value like

现在我想把“开罗”作为获得其价值的关键

$lang[] = array('Cairo' => "Cairo city");

the error appear when do this

执行此操作时出现错误

echo $lang[$key];

and its give me the "Warning: Illegal offset type" because of the key is SimpleXMLElementhow i can make it normal var?

它给我“警告:非法偏移类型”,因为关键是SimpleXMLElementhow我可以使它正常变量?

1 个解决方案

#1


9  

Cast it to a string with (string)$key.

使用(string)$ key将其强制转换为字符串。

#1


9  

Cast it to a string with (string)$key.

使用(string)$ key将其强制转换为字符串。