dps43378 2016-01-02 13:56 采纳率: 0%
浏览 32
已采纳

SimpleXML - 按元素属性值组

well am stuck again I am trying to group xml data based on the attribute 'name' value

XML Feed Return Data

 <Sport sport_code="FOOT" name="Football" disporder="-1000">
<SBClass sb_class_id="12430" disporder="-999" name="France">
<SBType sb_type_id="26463" name="France - Coupe de France" disporder="52">
<Ev inplay_allowed="Y" status="A" name="Gazélec Fco Ajaccio v Sainte Marienne" start_time="2016-01-02T12:30:00" virtual="N" ev_timezone="Europe/London" inplay_now="Y" mkt_count="37" ev_id="3400770" disporder="-9996">
<EvDetail br_match_id="8548770"/>
<Inplay inplay_period_num="1" clock_status="TICKING" correct_at="2016-01-02T12:50:11" inplay_period="first_half" score_string="0-0" last_change="2016-01-01T09:45:07" inplay_secs="1209"/>
<Teams>
<Team team_id="3411" team_order="0" name="Gazélec Fco Ajaccio" short_name="Gazélec Fco Ajaccio"/>
<Team team_id="172231" team_order="1" name="Sainte Marienne" short_name="Sainte Marienne"/>
</Teams>
</Ev>
</SBType>
</SBClass>

<SBClass sb_class_id="12430" disporder="-999" name="UK">
<SBType sb_type_id="26463" name="France - Coupe de France" disporder="52">
<Ev inplay_allowed="Y" status="A" name="Gazélec Fco Ajaccio v Sainte Marienne" start_time="2016-01-02T12:30:00" virtual="N" ev_timezone="Europe/London" inplay_now="Y" mkt_count="37" ev_id="3400770" disporder="-9996">
<EvDetail br_match_id="8548770"/>
<Inplay inplay_period_num="1" clock_status="TICKING" correct_at="2016-01-02T12:50:11" inplay_period="first_half" score_string="0-0" last_change="2016-01-01T09:45:07" inplay_secs="1209"/>
<Teams>
<Team team_id="3411" team_order="0" name="Gazélec Fco Ajaccio" short_name="Gazélec Fco Ajaccio"/>
<Team team_id="172231" team_order="1" name="Sainte Marienne" short_name="Sainte Marienne"/>
</Teams>
</Ev>
</SBType>
</SBClass>
</Sport>

I want it to group ALL data based on SBClass attribute 'name' value e.g. France, UK ( this value is dynamic btw so cant add fixed string values )

Am really struggling with this. have tried other examples but they only work if the data to group by is inside the node eg <node>DATA</node>

Hope someone can help with this

** Edit **

I have tried most of the grouping suggestions that are here on S.O.

    $xmlData = 'http://feeds-sports.winner.com/odds_feed?key=get_events_for_sport&sport_code=FOOT&on_date=2016-01-02';
$xml = simplexml_load_file($xmlData);

$leagues = $xml->xpath('/Sport/SBClass[@name="United Kingdom"]');
var_dump($leagues);    // var_dump #1

Its not even dumping anything at all

I want it to display the data like this Category : France ( or relevant value from SBClass attribute 'name' ) SBType name value : e.g France - Coupe de France Ev name value : e.g Gazélec Fco Ajaccio v Sainte Marienne

Then display all data for "France"

Then all data for Category : UK etc

** 2ND EDIT ** Here is the XML feed showing ALL data http://feeds-sports.winner.com/odds_feed?key=get_match_markets_for_sport&lang=en&sb_class_id=12430&sport_code=FOOT&mkt_sort=MRES

I want to group it by the value of SBType->attributes()->{'name'}

  • 写回答

1条回答 默认 最新

  • dongsimang4036 2016-01-02 16:29
    关注

    What you could do is loop the $xml->Sport->SBClass nodes and check if the name attribute exists.

    If the name attribute exists, you can add the item to an array (for example $sbClassGroups) and use the name attribute to create the array key for the group (France, Spain etc..)

    Now that you have the grouped data, you can loop this $sbClassGroups array using for example a foreach construct to get the values from the elements which are of type SimpleXMLElement.

    $xmlData = 'http://feeds-sports.winner.com/odds_feed?key=get_events_for_sport&sport_code=FOOT&on_date=2016-01-02';
    $xml = simplexml_load_file($xmlData);
    
    $sbClassGroups = array();
    
    foreach ($xml->Sport->SBClass as $item) {
        if (isset($item->attributes()->name)) {
            $sbClassGroups[(string)$item->attributes()->name][] = $item;
        }
    }
    
    foreach ($sbClassGroups as $sbClassGroup) {
        foreach( $sbClassGroup as $sbClass) {
            echo "<br><b>" . $sbClass->attributes()->name . "</b><br>";
    
            foreach ($sbClass->SBType as $sbt) {
                echo "<br>" . $sbt->attributes()->name . "<br>";
    
                foreach ($sbt->Ev as $ev) {
                    echo "<i>" . $ev->attributes()->name . "</i><br>";
                }
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line