PHP:按升序排序表NOT数据库表/模式

时间:2015-05-07 13:25:29

标签: php html foreach echo

我正在尝试订购一个用HTML和PHP编写和创建的表,为此我使用了一个循环,它为我提供了从XML文档中提供的信息。 例如,我可以从XML获得的是:

        
  1. 2:10 Romford
  2.     
  3. 1:50 Crayford
  4.     
  5. 6:30 Keswick
  6.     
  7. 4:25 Richmond
  8. 这些值都来自同一个XML文档,我不能(由于客户端的请求)存储并从数据库模式中提取信息。我正在构建表格:

    <?php
    $xml = simple_xml_load_file("xmlfeed");
    $item = $xml->items;
    ?>
    <table>
        <tbody>
            <?php
                foreach ($item as $key => $value) { echo "<tr><td>" . $value . "</td></tr>"; }
            ?>
        </tbody>
    </table>
    

    但是我需要能够根据开始的时间按升序排列它们,我该怎么做呢?

1 个答案:

答案 0 :(得分:0)

检查一下。我想你的代码可能会有所不同,但它确实需要......

$xml = simplexml_load_file('http://cachepricefeeds.williamhill.com/openbet_cdn?action=template&template=getHierarchyByMarketType&classId=3&marketSort=--&filterBIR=N');
$array = json_decode(json_encode((array)$xml), true); //convert to array
$list = [];
foreach ($array['response']['williamhill']['class']['type'] as $type => $typeData) {
    $typeId = $typeData['@attributes']['id'];
    $list[$typeId] = [
        'attributes' => $typeData['@attributes'],
        'market' => [],
    ];

    foreach ($typeData['market'] as $game) {
        $list[$typeId]['market'][] = $game;
    }

    usort($list[$typeId]['market'], function ($a, $b) {
        $aTime = strtotime($a['@attributes']['date'].' '. $a['@attributes']['time']);
        $bTime = strtotime($b['@attributes']['date'].' '. $b['@attributes']['time']);
        if ($aTime == $bTime) return 0;
        return ($aTime > $bTime) ? 1 : -1;
    });

}