Using usort with simplexml

2019-04-15 07:23发布

问题:

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...

回答1:

  • 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.



回答2:

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']