基于属性值对XML数据进行排序

时间:2016-01-05 16:02:40

标签: php arrays xml sorting

需要帮助的人 我设法找到了一个更简单的XML提要,现在可以使用它的完美

唯一的问题是它没有按日期

返回结果

这是xml文件

<event name="1m Maiden Stakes" eventid="5079164.20" date="20160105" time="1840" meeting="PORNICHET" venue="PORNICHET">
<bettype bet-start-date="20160105" bet-start-time="1840" ewreduction="5" ewplaceterms="3" eachway="1/5 Odds Place 1,2,3" suspended="false" name="Outright Betting" inrunning="0" bettypeid="25369679.20">
<bet jockey-silk="UqoAoo" name="Vision D'or" short-name="Vision D'" id="264074276.20" price="5/4" priceDecimal="2.25" priceUS="125.00" active-price-types="SP,EP"/>
<bet jockey-silk="MqoAoo" name="Le Moulleau" short-name="Le Moulle" id="264074277.20" price="10/1" priceDecimal="11.00" priceUS="1000.00" active-price-types="SP,EP"/>
<bet jockey-silk="TiqGii" name="Brancaio" short-name="Brancaio" id="264074278.20" price="10/1" priceDecimal="11.00" priceUS="1000.00" active-price-types="SP,EP"/>
<bet jockey-silk="PqlAqq" name="Bracoeur" short-name="Bracoeur" id="264074279.20" price="16/1" priceDecimal="17.00" priceUS="1600.00" active-price-types="SP,EP"/>
<bet jockey-silk="LoqGoo" name="Never Compromise" short-name="Never Com" id="264074280.20" price="12/1" priceDecimal="13.00" priceUS="1200.00" active-price-types="SP,EP"/>
<bet jockey-silk="HrbAbr" name="Machistador" short-name="Machistad" id="264074281.20" price="9/1" priceDecimal="10.00" priceUS="900.00" active-price-types="SP,EP"/>
<bet jockey-silk="UkkRkk" name="Royal Hawk" short-name="Royal Haw" id="264074282.20" price="8/1" priceDecimal="9.00" priceUS="800.00" active-price-types="SP,EP"/>
<bet jockey-silk="AqqGoo" name="Normandy Kitten" short-name="Normandy" id="264074283.20" price="9/2" priceDecimal="5.50" priceUS="450.00" active-price-types="SP,EP"/>
<bet jockey-silk="OqpAii" name="Tour" short-name="Tour" id="264074284.20" price="25/1" priceDecimal="26.00" priceUS="2500.00" active-price-types="SP,EP"/>
<bet jockey-silk="UqhRhq" name="Beninio" short-name="Beninio" id="264074285.20" price="16/1" priceDecimal="17.00" priceUS="1600.00" active-price-types="SP,EP"/>
<bet jockey-silk="OqoGoo" name="Just You And Me" short-name="Just You" id="264074286.20" price="33/1" priceDecimal="34.00" priceUS="3300.00" active-price-types="SP,EP"/>
</bettype>
</event>
<event name="1m Stakes " eventid="5079167.20" date="20160105" time="1910" meeting="PORNICHET" venue="PORNICHET">
<bettype bet-start-date="20160105" bet-start-time="1910" ewreduction="5" ewplaceterms="3" eachway="1/5 Odds Place 1,2,3" suspended="false" name="Outright Betting" inrunning="0" bettypeid="25369682.20">
<bet name="Blue Kanto" short-name="Blue Kant" id="264074319.20" price="10/1" priceDecimal="11.00" priceUS="1000.00" active-price-types="SP,EP"/>
<bet jockey-silk="ToqGoo" name="Francky Minuty" short-name="Francky M" id="264074320.20" price="16/1" priceDecimal="17.00" priceUS="1600.00" active-price-types="SP,EP"/>
<bet jockey-silk="CmkAkk" name="Rizzichop" short-name="Rizzichop" id="264074321.20" price="12/1" priceDecimal="13.00" priceUS="1200.00" active-price-types="SP,EP"/>
<bet jockey-silk="YblQlb" name="Colibri Cael" short-name="Colibri C" id="264074322.20" price="12/1" priceDecimal="13.00" priceUS="1200.00" active-price-types="SP,EP"/>
<bet jockey-silk="CibGii" name="Ali Alexandra" short-name="Ali Alexa" id="264074323.20" price="12/1" priceDecimal="13.00" priceUS="1200.00" active-price-types="SP,EP"/>
<bet name="Nayfah" short-name="Nayfah" id="264074324.20" price="4/5" priceDecimal="1.80" priceUS="-80.00" active-price-types="SP,EP"/>
<bet jockey-silk="RhmHmh" name="Elusive Dancer" short-name="Elusive D" id="264074326.20" price="4/1" priceDecimal="5.00" priceUS="400.00" active-price-types="SP,EP"/>
<bet jockey-silk="GanAaa" name="Kersea" short-name="Kersea" id="264074327.20" price="20/1" priceDecimal="21.00" priceUS="2000.00" active-price-types="SP,EP"/>
<bet jockey-silk="SlbGll" name="La Reussite" short-name="La Reussi" id="264074328.20" price="20/1" priceDecimal="21.00" priceUS="2000.00" active-price-types="SP,EP"/>
<bet jockey-silk="UlqAlb" name="Dream Cat" short-name="Dream Cat" id="264074329.20" price="16/1" priceDecimal="17.00" priceUS="1600.00" active-price-types="SP,EP"/>
</bettype>
</event>

这是我的PHP代码

$xmlData = 'http://xml.betfred.com/Horse-Racing-Daily.xml';
$xml = simplexml_load_file($xmlData);
$curdate = date('d/m/Y');
$i = 0;
    foreach ($xml->event as $event)
        if ($i < 5) {
        {
            //20160112
        $eventd = substr($event->attributes()->{'date'},6,2);
        $eventm = substr($event->attributes()->{'date'},4,2);
        $eventy = substr($event->attributes()->{'date'},0,4);
        $eventdate = $eventd.'/'.$eventm.'/'.$eventy;
        $eventid = $event->attributes()->{'eventid'};
        $eventtime = $event->attributes()->{'time'};
        $eventname = $event->attributes()->{'name'};
        echo "<a href=\"event/".$eventname."/".$eventid."\">".$event->attributes()->venue.' - '.$eventtime.' - '.$eventname."</a><br />".$eventdate."<br />";
        $i++;} }

当前它只显示前5个结果,但是我需要按$ eventtime值排列和显示数据,即$ event-&gt; attributes() - &gt; {'time'} ;值

在对这些xml信息进行过多的压力之后,请帮助那些我几乎就在那里的人

提前致谢

1 个答案:

答案 0 :(得分:0)

在这种情况下我会做什么,如果我没有办法改变数据库输出数据的方式,我会把它作为关联数组加载到PHP中并对键进行排序。像这样:

<?php 

$xmlData = 'http://xml.betfred.com/Horse-Racing-Daily.xml';
$xml = simplexml_load_file($xmlData);
$curdate = date('d/m/Y');

$new_array = array();
$limit = 5;
$c = 0;
foreach ($xml->event as $event) {
    if ($limit == $c) {
        break;
    }
    $c++;
    $eventd = substr($event->attributes()->{'date'},6,2);
    $eventm = substr($event->attributes()->{'date'},4,2);
    $eventy = substr($event->attributes()->{'date'},0,4);
    $eventt = $event->attributes()->{'time'};
    $new_array[$eventy.$eventm.$eventd.$eventt] = array(
        'eventd' => $eventd,
        'eventm' => $eventm,
        'eventy' => $eventy,
        'eventt' => $eventt,
        'eventdate' => $eventd.'/'.$eventm.'/'.$eventy,
        'eventid' => $event->attributes()->{'eventid'},
        'eventtime' => $event->attributes()->{'time'},
        'eventname' => $event->attributes()->{'name'},
        'venue' => $event->attributes()->venue,
    );
}

ksort($new_array);
foreach ($new_array as $event_time => $event_data) {
    echo "<a href=\"event/".$event_data['eventname']."/".$event_data['eventid']."\">".$event_data['venue'].' - '.$event_data['eventtime'].' - '.$event_data['eventname']."</a><br />".$event_data['eventdate']."<br />";
}

当然这不是测试。

编辑 - 已经过测试,现在可以正常使用。

重点是您首先使用所需信息加载数组,按关联数组的键排序,然后打印出您想要的内容。您可以更改完整加载列表的限制,或仅更改xml文件中的前5个限制。祝你好运!