SimpleXML动态元素名称

2022-04-13 00:00:00 php simplexml
echo $xml->SLOT1->Effect;
echo $xml->SLOT2->Effect;
echo $xml->SLOT3->Effect;
有没有办法通过使用for循环来简化这一过程?我试过了,但没有回音:

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

解决方案

您可以使用

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

相关文章