English 中文(简体)
使用简明灯
原标题:Using usort with simplexml

我有一个问题,即我的价值观都没有按照正确的顺序结束。

        $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>";
        }

从我所说的一切来看,这都应奏效,但确实如此。 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>

我所看到的结果是,这根本不是秩序。

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

任何想法?

许多感谢......

最佳回答
  • usort accepts array.
  • When you compare two SimpleXMLElements, you should cast them.

因此修改法典

$sort=$x->person;

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

纽约总部

$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;
}

将给你适当的结果。

问题回答

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

现在,你可以通过<条码><>t$sort 至>>>usort,并可领取工作罚款。 http://code>$a->age with $a [ age]





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签