php - Sorting an array of SimpleXML objects -


I've read what I found on stackoverflow and is still unclear on this.

i is an array of some simpleXML objects like this:

  array (2) {[0] => Object (SimplifiedMailment) # 2 (2) {["name"] => String (15) "Andrew" ["age"] = & gt; String (2) "21"} [1] = & gt; Object (simple assemment) # 3 (2) {["name"] = & gt; String (12) "Beth" ["age"] = & gt; String (2) "56"}}  

and I want to be able to sort by sorting, ascending or descending. Something like this:

  sort ($ data, 'name', 'acc');  

How do I pass an object over the array and sort by the way I like it, based on its value.

For reference, one identical. The NET solution will be with these lines:

  XmlSortOrder command = XmlSortOrder.Ascending; If (sortDirection == "ACC") {order = XmlSortOrder.Ascending; } Expression. Edset (column settingon + "/ text ()", order, XML orders order, "N-U", XML DTD type.);  

I have seen people

"use usort"

According to a basic example from PHP Manual but it does not really explain this. At least not to me. I have seen that people get the suggestion of using an external library like simple DOM, but I want to avoid some external use for it (seems to be, though I can not solve it yet)

Any help is appreciated, thanks!

I think people would suggest using me :)

I have written SimpleDOM :: sort () for that situation, because you need to use simple sorting by assertive expression (or arbitrary expression) that is boring and does not teach you anything useful.

This is a short version of how it works: First you create a simple array of key => value pairs, which is done with every simple assemble and the value with which it will be sorted. In your example, if you send them to & lt; Age / & gt; , then the array will be the array (21, 56) . After that you call array_multisort () as the first argument with "proxy array" followed by any number such as SORT_DESC or SORT_NUMERIC, in the end you want to sort the array , Which will be passed by reference. / P>

You will end up with something like this:

  $ nodes = array (new SimpleXMLElement ('& lt; person & gt; & lt; name & gt; Andrew & lt ; & Lt; man & gt; & lt; 21 & lt; / age & gt; & lt; / person & gt; '); Gt; Beth & lt; / name & gt; & gt; Age & gt; 56 & lt; / age & gt; & lt; / person & gt; ')); Function xsort (and $ nodes, $ child_name, $ order = SORT_ASC) {$ sort_proxy = array (); Forex currency ($ c => $ node as node) {$ sort_proxy [$ k] = (string) $ node- & gt; $ Child_name; } Array_multisort ($ sort_proxy, $ command, $ nodes); } Xsort ($ nodes, 'name', SORT_ASC); Print_r ($ nodes); XSort ($ nodes, 'age', SORT_DESC); Print_r ($ nodes);  

But in reality, instead of burdening you with more code, you have to maintain and potentially change array_multisort () to userspace, you are currently Should take advantage of the solution. Such sorting algorithm / routine does not have any particular interesting thing, if your time does not already exist, then it will be better.


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -