Sorting SimpleXML Entries by Single Key

Given the following XML that has been loaded into a SimpleXML object in PHP:

To sort by name, do the following:

To sort by city, do the following:

This is a simple technique that works when you have a single key by which you wish to sort the XML. We create the $data array and associate the key (name in the former case, city in the latter) with the actual element, and let ksort take care of the rest. ksort sorts an array by the key while maintaining the association.
Continue reading “Sorting SimpleXML Entries by Single Key”