Norse
Norse

Reputation: 5757

SimpleXML Dynamic element names

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;
}

Upvotes: 1

Views: 1069

Answers (2)

pogeybait
pogeybait

Reputation: 3145

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

Upvotes: 1

Taha Paksu
Taha Paksu

Reputation: 15616

You can use

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

Upvotes: 3

Related Questions