user3382209
user3382209

Reputation: 21

How to find other children's value using simplexml

<playerstats>
 <stats>
  <stat>
   <name>total_kills</name>
   <value>9999</value>
  </stat>
  <stat>
   <name>total_deaths</name>
   <value>1234</value>
  </stat>
 </stats>
</playerstats>

How do I search for "total_kills" to get the value "9999" using simplexml in php?

Upvotes: 1

Views: 30

Answers (1)

You could make use of simplexml_load_string in PHP.

<?php
$xml='<playerstats>
 <stats>
  <stat>
   <name>total_kills</name>
   <value>9999</value>
  </stat>
  <stat>
   <name>total_deaths</name>
   <value>1234</value>
  </stat>
 </stats>
</playerstats>';
$xml = simplexml_load_string($xml);
foreach ($xml->stats->stat as $child)
{
 if($child->name=='total_kills')
 {
     echo $child->value;
 }
}

OUTPUT :

9999

Upvotes: 1

Related Questions