note 81931 added to function.simplexml-element-children

From: Date: Wed, 19 Mar 2008 22:37:33 +0000
Subject: note 81931 added to function.simplexml-element-children
Groups: php.notes 
Request: Send a blank email to php-notes+get-137218@lists.php.net to get a copy of this message
I made a slightly differnt approch towards the RecurseXML function. Beeing hungry I had problems with the code, as it did just overwrite two <maincourse>s. So here is what I did: <?php $xml = new SimpleXMLElement( '<meal> <type>Lunch</type> <time>12:30</time> <menu> <entree>salad</entree> <maincourse> <part>ships</part> <part>steak</part> </maincourse> <maincourse> <part>fisch</part> <part>rice</part> </maincourse> <maincourse> <part>wine</part> <part>cheese</part> </maincourse> </menu> </meal>'); $vals = array(); RecurseXML($xml,$vals); foreach($vals as $key=>$value) print("{$key} = {$value}<BR>\n"); function RecurseXML($xml,&$vals,$parent="") { $childs=0; $child_count=-1; # Not realy needed. $arr=array(); foreach ($xml->children() as $key=>$value) { if (in_array($key,$arr)) { $child_count++; } else { $child_count=0; } $arr[]=$key; $k=($parent == "") ? "$key.$child_count" : "$parent.$key.$child_count"; $childs=RecurseXML($value,$vals,$k); if ($childs==0) { $vals[$k]= (string)$value; } } return $childs; } ?> Output is like this: type.0 = Lunch time.0 = 12:30 menu.0.entree.0 = salad menu.0.maincourse.0.part.0 = ships menu.0.maincourse.0.part.1 = steak menu.0.maincourse.0 = menu.0.maincourse.1.part.0 = fisch menu.0.maincourse.1.part.1 = rice menu.0.maincourse.1 = menu.0.maincourse.2.part.0 = wine menu.0.maincourse.2.part.1 = cheese menu.0.maincourse.2 = menu.0 = (Not beautiful, but it solved my case...) ---- Server IP: 69.147.83.197 Probable Submitter: 87.67.92.70 ---- Manual Page -- http://www.php.net/manual/en/function.simplexml-element-children.php Edit -- https://master.php.net/note/edit/81931 Del: integrated -- https://master.php.net/note/delete/81931/integrated Del: useless -- https://master.php.net/note/delete/81931/useless Del: bad code -- https://master.php.net/note/delete/81931/bad+code Del: spam -- https://master.php.net/note/delete/81931/spam Del: non-english -- https://master.php.net/note/delete/81931/non-english Del: in docs -- https://master.php.net/note/delete/81931/in+docs Del: other reasons-- https://master.php.net/note/delete/81931 Reject -- https://master.php.net/note/reject/81931 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#137218) next »