Count of elements in XML

From: Date: Fri, 20 Jan 2006 17:36:11 +0000
Subject: Count of elements in XML
Groups: php.general 
Request: Send a blank email to php-general+get-228963@lists.php.net to get a copy of this message
I was wondering if there is a way to get the number of foo elements in my XML below? This way I could loop through each element and print out the attributes. $string = "<a> <foo name=\"one\" game=\"lonely\">1</foo> <foo name=\"two\" game=\"again\">2</foo> <foo name=\"three\" game=\"blah\">3</foo> <foo name=\"four\" game=\"goodness\">4</foo> </a>"; $xml = simplexml_load_string($string); // this doesn't return the correct number of elements but instead returns the data inbetween <foo></foo> $cnt = count($xml->foo); for($i=0; $i<$cnt; $i++) { foreach($xml->foo[$i]->attributes() as $a => $b) { echo $a,'="',$b,"\"\n"; } }

« previous php.general (#228963) next »