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 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色