Need help sorting a XML file

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I have a similar xml file and I redisplay it with the following array in  
PHP. What I would like to do is sort the <id> field before it is  
redisplayed. Looking for a sample code.



$url = "test.xml";
$data = implode("", file($url));
preg_match_all ("/<item>([^`]*?)<\/item>/", $data, $matches);

// Begin feed
header ("Content-Type: text/xml; charset=ISO-8859-1");
echo "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n";



foreach ($matches[0] as $match) {

  preg_match ("/<id>([^`]*?)<\/id>/", $match, $temp);
  $id = $temp['1'];

  preg_match ("/<name>([^`]*?)<\/name>/", $match, $temp);
  $description = $temp['1'];

// Echo RSS XML
   echo "<item>\n";
   echo "\t\t\t<id>" .  $id . "</id>\n";
   echo "\t\t\t<name>" . $name . "</name>\n";
   echo "\t\t</item>\n";


Re: Need help sorting a XML file

Quoted text here. Click to load it

Well, I think you mean to use $description in that second to last echo
statement, but that's not your question. If your ID's are unique, why
not build an array that maps the ID to the data (in this case just a
single string) and then you can sort and display as you please?

Site Timeline