simplexml sorting data

Gosman
Gosman used Ask the Experts™
on
I'm trying to sort an xml file (which I have no control over) alphabetically. Is there a way to do this using simplexml?

Basically I'm trying to create a dropdown list using the xml returned, the dropdown needs to be sorted alphabetically by <description>

From the xml attached this is what I would like to achieve

<select>
<option value="gr-tx">Barryscourt Castle</option>
<option value="ab-hy">Cork Heritage Park</option>
</select>
<locations>
   <location>
    <description>Cork Heritage Park</description>
	<id>ab-hy</id>
  </location>
  <location>
	<description>Barryscourt Castle</description>
    <id>gr-tx</id>
  </location>
</locations>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
<?php

$xml = '<locations>
   <location>
    <description>Cork Heritage Park</description>
	<id>ab-hy</id>
  </location>
  <location>
	<description>Barryscourt Castle</description>
    <id>gr-tx</id>
  </location>
</locations>';


preg_match_all("#<description>(.+?)</description>#", $xml, $ddesc);
preg_match_all("#<id>(.+?)</id>#", $xml, $did);
$fdesc = array();
$fid = array();
for($i = 0; $i <= count($ddesc[0]); $i++) {
array_push($fdesc, $ddesc[0][$i]);
}
for($i = 0; $i <= count($did[0]); $i++) {
array_push($fid, $did[0][$i]);
}
sort($fdesc);
sort($fid);

echo '<select>';
for($i = 1; $i < count($fdesc); $i++) {
  echo '<option value="'.$fid[$i].'">'.$fdesc[$i].'</option>';
}
echo '</select>';
?>

Open in new window

or if you call the file externally from other site.. the code will be this:

<?php

$xml = file_get_contents("http://mysite.com/my_xml_file.xml");

preg_match_all("#<description>(.+?)</description>#", $xml, $ddesc);
preg_match_all("#<id>(.+?)</id>#", $xml, $did);
$fdesc = array();
$fid = array();
for($i = 0; $i <= count($ddesc[0]); $i++) {
array_push($fdesc, $ddesc[0][$i]);
}
for($i = 0; $i <= count($did[0]); $i++) {
array_push($fid, $did[0][$i]);
}
sort($fdesc);
sort($fid);

echo '<select>';
for($i = 1; $i < count($fdesc); $i++) {
  echo '<option value="'.$fid[$i].'">'.$fdesc[$i].'</option>';
}
echo '</select>';

Open in new window

Does it work ??
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

Author

Commented:
Perfect.

Just had to change $ddesc[0] & $did[0] to $ddesc[1] & $did[1] so the tags don't get included in the dropdown.

Author

Commented:
Actually V4nP3rs13 it doesn't work.

It only appears to work if the list has  2 <location> nodes. Any more than this and the description and id don't match. See code attached.

Any ideas?
<?php

$xml = '<locations>
   <location>
    <description>Cork Heritage Park</description>
	<id>ab-hy</id>
  </location>
  <location>
	<description>Barryscourt Castle</description>
    <id>gr-tx</id>
  </location>
    <location>
	<description>Bunratty Castle</description>
    <id>bc-xy</id>
  </location>

</locations>';


preg_match_all("#<description>(.+?)</description>#", $xml, $ddesc);
preg_match_all("#<id>(.+?)</id>#", $xml, $did);
$fdesc = array();
$fid = array();
for($i = 0; $i <= count($ddesc[0]); $i++) {
array_push($fdesc, $ddesc[0][$i]);
}
for($i = 0; $i <= count($did[0]); $i++) {
array_push($fid, $did[0][$i]);
}
sort($fdesc);
sort($fid);

echo '<select>';
for($i = 1; $i < count($fdesc); $i++) {
  echo '<option value="'.$fid[$i].'">'.$fdesc[$i].'</option>';
}
echo '</select>';
?>

Open in new window

oh I knew it must me a problem... i will recode this and post you another code

Author

Commented:
Don't worry V4nP3rs13 I managed to sort the problem by using simplexml and a multi dimensional array.

Thank you anyway
Now it works:
<?php
$xml = '<locations>
   <location>
    <description>Cork Heritage Park</description>
	<id>ab-hy</id>
  </location>
  <location>
	<description>Barryscourt Castle</description>
    <id>gr-tx</id>
  </location>
    <location>
	<description>Bunratty Castle</description>
    <id>bc-xy</id>
  </location>

</locations>';


preg_match_all("#<description>(.+?)</description>#", $xml, $fdesc);
preg_match_all("#<id>(.+?)</id>#", $xml, $fid);

$item = array();

for($i = 0; $i <= count($fdesc[0]); $i++) {
  $item[$i] = $fdesc[0][$i]."'||-item-||'".$fid[0][$i];
}
sort($item);

echo '<select>';
for($i = 1; $i < count($item); $i++) {
  list($b[$i], $a[$i]) = explode("'||-item-||'", $item[$i]);
  echo '<option value="'.str_ireplace(array("<id>", "</id>"), null, $a[$i]).'">'.$b[$i].'</option>';
}
echo '</select>';
?>

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial