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 App的会员连续扣费
  • ¥15 不同数据类型的特征融合应该怎么做
  • ¥15 用proteus软件设计一个基于8086微处理器的简易温度计
  • ¥15 用联想小新14Pro
  • ¥15 multisim中关于74ls192n和DSWPK开关仿真图分析(减法计数器)
  • ¥15 基于8255的交通灯设计
  • ¥15 w3wp,exe 中发生未处理的 Microsoft ,NETFramework 异常。
  • ¥20 C51单片机程序及仿真(加减器)
  • ¥15 AQWA | 水动力分析 二阶波浪力
  • ¥15 c语言题目:成绩管理系统