I read what I found in Stackoverflow, and is still unclear.
I have an array of SimpleXML objects like this:
array(2) { [0]=> object(SimpleXMLElement)#2 (2) { ["name"]=> string(15) "Andrew" ["age"]=> string(2) "21" } [1]=> object(SimpleXMLElement)#3 (2) { ["name"]=> string(12) "Beth" ["age"]=> string(2) "56" } }
And I want to be able to sort by any column, ascending or descending. Something like:
sort($data, 'name', 'asc');
Where can I go in the array of objects above and sort by the value of what I like.
For reference, a similar .NET solution would be as follows:
XmlSortOrder order = XmlSortOrder.Ascending; if ( sortDirection == "asc" ) { order = XmlSortOrder.Ascending; } expression.AddSort( columnSortingOn + "/text()", order, XmlCaseOrder.UpperFirst, "en-us", XmlDataType.Text );
I saw people say
"Use usort"
The following is a basic example from a PHP manual, but that doesn't really explain it. At least not for me. I also saw that people suggest using an external library such as SimpleDOM, but I want to avoid using something external for this (it would seem, although I still can not solve it).
Any help is appreciated, thanks!
sorting php simplexml
Stuart
source share