SimpleXML Dynamic element names

两盒软妹~` 提交于 2019-12-02 09:56:17

问题


echo $xml->SLOT1->Effect;
echo $xml->SLOT2->Effect;
echo $xml->SLOT3->Effect;

Is there a way to simplify this by using a for loop? I tried this but it echos nothing:

for ($x = 1; $x <= 3; $x++) {
   echo $xml->SLOT[$x]->Effect;
}

回答1:


You can use

$xml->{"SLOT".$x}->Effect;



回答2:


for ($x = 1; $x <= 3; $x++) {
   echo $xml->{SLOT.$x}->Effect;
}


来源:https://stackoverflow.com/questions/10662795/simplexml-dynamic-element-names

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!