Using usort with simplexml

I'm having a problem where none of my values are ending up in the right order.

        $xml = file_get_contents('admin/people.xml');
        $x = new SimpleXMLElement($xml);

        $sort=$x->person;

        function cmp($a, $b){
            if ($a->age == $b->age) {
                return 0;
            }
            return ($a->age < $b->age) ? -1 : 1;
        }
        usort($sort, 'cmp');

        foreach ($sort as $key => $value) {
            echo "$key: $value->age<br>";
        }

From everything I've read, this should work, but it doesn't. Here is the XML:

        <people>
            <person>
                <name>Frank</name>
                <age>12</age>
            </person>
            <person>
                <name>Jim</name>
                <age>6023</age>
            </person>
            <person>
                <name>Tony</name>
                <age>234</age>
            </person>
            <person>
                <name>Bob</name>
                <age>2551</age>
            </person>
            <person>
                <name>Dave</name>
                <age>21</age>
            </person>
            <person>
                <name>Trevor</name>
                <age>56</age>
            </person>
            <person>
                <name>Mike</name>
                <age>89</age>
            </person>
        </people>

And the result I'm getting is this, which is no kind of order at all!

0: 6023
2: 21
3: 234
4: 12
6: 56
7: 2551
8: 89

Any ideas?

Many thanks...

Answers


  • usort accepts array.
  • When you compare two SimpleXMLElements, you should cast them.

So change the code

$sort=$x->person;

function cmp($a, $b){
    if ($a->age == $b->age) {
        return 0;
    }
    return ($a->age < $b->age) ? -1 : 1;
}

to

$sort = array();
foreach ($x->person as $person) {
        $sort[] = $person;
}

function cmp($a, $b){
    if ((int)$a->age == (int)$b->age) {
        return 0;
    }
    return ((int)$a->age < (int)$b->age) ? -1 : 1;
}

will give you the right result.


In order to use usort you'll need to convert your SimpleXMLElement to array. Here is a quick way to do that (http://www.php.net/manual/en/book.simplexml.php#105330):

$xml = file_get_contents('admin/people.xml');
$x = new SimpleXMLElement($xml);
$json = json_encode($x);
$xml_array = json_decode($json,TRUE);
$sort = $xml_array['person'];

Now you can pass $sort to usort and it'll work fine. Replace $a->age with $a['age']


Need Your Help

Not checking validation moving to next activity in android

android validation

Hi In My Application I have username and password and login button.click the login submit button without checking validations and empty fields also moving to next activity.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.