Sorting an array of SimpleXML objects

Amr Ragaey

New Member
I've read what I've found on Stackoverflow and am still unclear on this.I have an array of SimpleXML objects something like this:\[code\]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" }}\[/code\]And I want to be able to sort by whatever column, ascending or descending. Something like:\[code\]sort($data, 'name', 'asc');\[/code\]Where I can pass in the above array of objects and sort by the value of whichever key I like. For reference, a similar .NET solution would be along these lines:\[code\]XmlSortOrder order = XmlSortOrder.Ascending; if ( sortDirection == "asc" ) { order = XmlSortOrder.Ascending; } expression.AddSort( columnSortingOn + "/text()", order, XmlCaseOrder.UpperFirst, "en-us", XmlDataType.Text ); \[/code\]I've seen people say \[quote\] "Use usort"\[/quote\]Followed by a basic example from the PHP manual but this doesn't really explain it. At least not to me. I've also seen people suggest using an external library like SimpleDOM but I want to avoid using something external for this (seemingly, though I cannot yet solve it) little thing.Any help is appreciated, Thanks!
 
Back
Top