Jump to content

Sorting SimpleXML


lj-

Recommended Posts

Hello,

 

Hope somebody can help me.

I have an XML file that is generated by active directory that dumps everything required for a telephone directory.

I currently present this using XSL but would like to move towards PHP.

 

I have some rudimentary code and it's working fine but I'd like to be able to sort by various values, such as First Name or telephone number.

 

My XML format looks like this:

 

<?xml version="1.0" encoding="UTF-8"?>
<telephonelist>
   <employee>
      <firstname>Jimmy</firstname>
      <lastname>Jimmerson</lastname>
      <email>[email protected]</email>
      <extensionnum>956</extensionnum>
      <jobtitle>Security Officer</jobtitle>
      <department>Security</department>
   </employee>
   <employee>
      <firstname>David</firstname>
      <lastname>Davidson</lastname>
      <email>[email protected]</email>
      <extensionnum>968</extensionnum>
      <jobtitle>Manager</jobtitle>
      <department>Management</department>
   </employee>

 

Any my current code looks like this:

 

<?
$myStaffMembers = new SimpleXMLElement("includes/telephonelist.xml", null, true);

echo <<<EOF
<table width="95%" border="0">
  <tr>                
<th><b>First Name</b></th>                
<th><b>Last Name</b></th>                
<th><b>Telephone Number</b></th>                
<th><b>Position</b></th>                       
  </tr>
EOF;

foreach($myStaffMembers as $myStaffMember) 
{
echo "      
  <tr>                
<td>{$myStaffMember->firstname}</td>                
<td>{$myStaffMember->lastname}</td>                
<td>{$myStaffMember->extensionnum}</td>                
<td>{$myStaffMember->jobtitle}</td>                       
  </tr>";
}
?>

 

Appreciate any help anybody could give. :)

 

Thanks,

LJ

Link to comment
https://forums.phpfreaks.com/topic/248224-sorting-simplexml/
Share on other sites

Hi silkfire,

 

Thank you for your response but as I mentioned, I know how to do this in XSL - I currently do it this way.

I'd like to switch to sorting within PHP so I only have to maintain one header/footer and just include() the XML (or any other) content in the middle.

 

Thanks,

Link to comment
https://forums.phpfreaks.com/topic/248224-sorting-simplexml/#findComment-1274650
Share on other sites

Since you don't need the values to remain in a object, I like to dump the values to array and sort using that.

 

Solution for only sorting by a single value:

<?php 

$xml_string = '<?xml version="1.0"?>
<telephonelist>
   <employee>
      <firstname>Jimmy</firstname>
      <lastname>Jimmerson</lastname>
      <email>[email protected]</email>
      <extensionnum>956</extensionnum>
      <jobtitle>Security Officer</jobtitle>
      <department>Security</department>
   </employee>
   <employee>
      <firstname>David</firstname>
      <lastname>Davidson</lastname>
      <email>[email protected]</email>
      <extensionnum>968</extensionnum>
      <jobtitle>Manager</jobtitle>
      <department>Management</department>
   </employee>
</telephonelist>';

$xml = new SimpleXMLElement( $xml_string );
$data = array();

$key = 0;
foreach( $xml as $group ) { // Loop through <employee>s
foreach( $group as $name => $entry ) // Loop through each tag in <employee>
	$data[$key][$name] = (string) $entry; // Record to array
$key++;
}

usort( $data, 'sortBy' );

print_r( $data );

function sortBy($a,$b) {
return $a['lastname'] > $b['lastname'];
}

?>

 

For multiple values, use array_multisort or a function similar to this: http://ca.php.net/manual/en/function.usort.php#105764

Link to comment
https://forums.phpfreaks.com/topic/248224-sorting-simplexml/#findComment-1274758
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.