PHP - For loop only returns last variable in array

mildredbrick

New Member
I have a strange problem where a for loop in PHP only returns the last item in my array.The array is created with SimpleXML from an XML file.The code should return this:\[code\]<tags><tag value="http://stackoverflow.com/questions/2011988/Tag1" /><tag value="http://stackoverflow.com/questions/2011988/Tag2" /><tag value="http://stackoverflow.com/questions/2011988/Tag3" /></tags>\[/code\]But instead I just get:\[code\]<tags><tag value="http://stackoverflow.com/questions/2011988/Tag3" /></tags>\[/code\]So it ignores all but the last item in the array no matter how many I got in there.Can anyone see what I'm doing wrong?Here's the code:\[code\]<?phpfunction gettags($xml){ $xmltags = $xml->xpath('//var[@name="infocodes"]/string'); return $xmltags[0];}//Path to the XML files on the server$path = "/xmlfiles/";//Create an array with all the XML files$files = glob("$path/*.xml");foreach($files as $file){ $xml = simplexml_load_file($file); $xmltags = gettags($xml);//Using the , character split the values coming from the $xmltags into an array$rawtags = explode(',', $xmltags);//Loop through the tags and add to a variable. Each tag will be inside an XML element - <tag value="http://stackoverflow.com/questions/2011988/tagname" />for ($i = 0; $i <count($rawtags); $i++){ $tags = '<tag value="' . $rawtags[$i] . '" />';}//Replace HTML escaped characters (?, ?, ?, ?, ?, ?) and the | character with normal characters in the tags variable$tagsunwantedchars = array("&Ouml;", "&Auml;", "&Aring;", "&ouml;", "&auml;", "&aring;", "|");$tagsreplacewith = array("?", "?", "?", "?", "?", "?", " - ");$tagsclean = str_replace($tagsunwantedchars, $tagsreplacewith, $tags);//Create the full tag list and store in a variable$taglist = "<tags>$tagsclean</tags>";}echo $taglist;?>\[/code\]Here's the XML file:\[code\]<wddxPacket version='1.0'> <header/> <data> <struct> <var name='infocodes'> <string>Tag1,Tag2,Tag3</string> </var> </struct> </data></wddxPacket>\[/code\]
 
Back
Top