user1695981
user1695981

Reputation: 89

SimpleXML Lower level variable

I'm parsing a file with simple_xml_load_file(), level by level. Here's the sample structure:

<person name="Joe Smith" ...>
   <info age="19">
     <height val="1.85" />
   </info>
   <info age="19">
     <weight val="82" />
   </info>
   <info age="19">
     <build val="14" />
   </info>
</person>
...

As I am parsing I am not going deep, as I don't need to. I do need the age however, without going through each info tag. I need the variables contained in <person> and only the age. How would I go about getting age without another loop?

$persons=$dom->person;
foreach($persons as $person){
   $name=$person['name'];
   $age=????
}

Upvotes: 0

Views: 42

Answers (1)

TiMESPLiNTER
TiMESPLiNTER

Reputation: 5899

This should do the job:

foreach($dom->person as $person){
   $name=$person['name'];
   foreach($person->info as $info) {
       echo $info['age'] . '<br>';
   }
}

Or if you want to get one age at a specific position:

echo $person->info[0]['age']; // Gets age attribute of first <info> node

Upvotes: 3

Related Questions