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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line