lj- Posted October 1, 2011 Share Posted October 1, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/248224-sorting-simplexml/ Share on other sites More sharing options...
silkfire Posted October 1, 2011 Share Posted October 1, 2011 In XSL: http://www.velocityreviews.com/forums/t169533-sort-xml-and-write-to-xml.html Quote Link to comment https://forums.phpfreaks.com/topic/248224-sorting-simplexml/#findComment-1274649 Share on other sites More sharing options...
lj- Posted October 1, 2011 Author Share Posted October 1, 2011 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, Quote Link to comment https://forums.phpfreaks.com/topic/248224-sorting-simplexml/#findComment-1274650 Share on other sites More sharing options...
xyph Posted October 1, 2011 Share Posted October 1, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/248224-sorting-simplexml/#findComment-1274758 Share on other sites More sharing options...
lj- Posted October 2, 2011 Author Share Posted October 2, 2011 Morning xyph, That is exactly what I was looking for - why didn't I think of stuffing it all in an array? Thanks very much for your help. Quote Link to comment https://forums.phpfreaks.com/topic/248224-sorting-simplexml/#findComment-1274841 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.