radox Posted October 10, 2009 Share Posted October 10, 2009 I'm using xml_parse_into_struct to get all my elements, but now I need to group them. For example, here's the XML <categories> <category id="12345"> <title>category title 1</title> <tips> <tip id="0"> <title>hello world</title> <content>some content</content> </tip> <tip id="1"> <title>foo</title> <content>foo content</content> </tip> </tips> </category> <category id="7890"> <title> category title 2</title> <tips> <tip id="2"> <title>another tip title</title> <content>blah blah</content> </tip> <tip id="3"> <title>another tip title</title> <content>blah blah</content> </tip> </tips> </category> </categories> How would I just grab the tip titles for the 2nd category (7890)? Hope I'm making sense. I can loop through the array using foreach, but have no way to determine if a tip belongs to the 1st or 2nd category. Ideas?? Please help! Thanks Link to comment https://forums.phpfreaks.com/topic/177157-xml-grouping/ Share on other sites More sharing options...
radox Posted October 20, 2009 Author Share Posted October 20, 2009 Here's the solution: //open xml files $xmlperfile = 'file.xml'; //open personal xml file for processing $perfp = fopen($xmlperfile, "r") or die("Could not open file"); $perdata = fread($perfp, filesize($xmlperfile)) or die("Could not read file"); //create xml parser and extract personal data $xmlperparser = xml_parser_create(); xml_parser_set_option($xmlperparser, XML_OPTION_CASE_FOLDING, 0); xml_parser_set_option($xmlperparser, XML_OPTION_SKIP_WHITE, 1); xml_parse_into_struct($xmlperparser,$perdata,$pervals,$perindex); xml_parser_free($xmlperparser); //close both xml file reads fclose($perfp); $tipcat=0; foreach($pervals as $element){ if($element['level']==6){ if($element['tag']=='Title'){ $pertiptitle[$tipcat][]=$element['value']; } if($element['tag']=='Content'){ $pertipcontent[$tipcat][]=$element['value']; } } if($element['tag']=='Category'&&$element['type']=='close'){ $tipcat++; } } Link to comment https://forums.phpfreaks.com/topic/177157-xml-grouping/#findComment-940672 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.