duanlvji4780 2014-01-25 18:19
浏览 47
已采纳

PHP循环遍历XML文件,放入数组,然后排序

I have a folder of XML files that look like this, all with different timestamps

<?xml version="1.0"?>
<comment>
  <timestamp>1390601221</timestamp>
</comment>

I'm using the glob function to put all of these into an array

$xmls = glob("xml/*.xml");

Then I'm trying to put the timestamp value and xml path into a new array so I can sort by the timestamp. This is how I'm doing it.

$sorted_xmls = array();

        foreach ($xmls as $xml) {

            $raw_xml = file_get_contents($xml);

            $data = simplexml_load_string($raw_xml);

            $time = $data->timestamp;
            array_push($sorted_xmls, array($time, $xml));

        }

All of this seems to work fine. Now I want to sort by timestamp. With the newest first.

foreach ($sorted_xmls as $key => $row) {
    $final_sorted[$key]  = $row[0];
}


array_multisort($final_sorted, SORT_ASC);

It doesn't seem to be working as expected. Am I doing something wrong? I assume it's on the sorting portion

  • 写回答

1条回答 默认 最新

  • dozabt4329 2014-01-25 18:39
    关注

    You are calling array_multisort() in the wrong way here. The way you need to call it is as in example #3 on the manual page, "sorting database results".

    The way this works is that you pass the "columns" you want to sort by, and the flags to sort that column by, in order, then pass the target array (the array that will actually be sorted) as the last argument.

    So if you change your last line to this:

    array_multisort($final_sorted, SORT_ASC, $sorted_xmls);
    

    ...then $sorted_xmls should be sorted in the way you would like it to be.

    However, a more efficient, albeit more complex, way to do this might be to sort the $xmls array directly using usort(), and load the files from disk at the same time.

    $xmls = glob("xml/*.xml");
    
    usort($xmls, function($a, $b) {
        // Temporary array to hold the loaded timestamps
        // Because this is declared static in a closure, it will be free'd when
        // the closure goes out of scope, i.e. when usort() returns
        // If you want to store the timestamps for use later, you can import a
        // reference to an external variable into the closure with a use() element
        static $timestamps = array();
    
        // Load XML from disk if not already loaded
        if (!isset($timestamps[$a])) {
            $timestamps[$a] = simplexml_load_file($a)->timestamp;
        }
        if (!isset($timestamps[$b])) {
            $timestamps[$b] = simplexml_load_file($b)->timestamp;
        }
    
        // Return values appropriate for sorting
        if ($timestamps[$a] == $timestamps[$b]) {
            return 0;
        }
        return $timestamps[$a] < $timestamps[$b] ? 1 : -1;
    });
    
    print_r($xmls);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?