dps43378 2016-01-02 05: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

  1. <Sport sport_code="FOOT" name="Football" disporder="-1000">
  2. <SBClass sb_class_id="12430" disporder="-999" name="France">
  3. <SBType sb_type_id="26463" name="France - Coupe de France" disporder="52">
  4. <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">
  5. <EvDetail br_match_id="8548770"/>
  6. <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"/>
  7. <Teams>
  8. <Team team_id="3411" team_order="0" name="Gazélec Fco Ajaccio" short_name="Gazélec Fco Ajaccio"/>
  9. <Team team_id="172231" team_order="1" name="Sainte Marienne" short_name="Sainte Marienne"/>
  10. </Teams>
  11. </Ev>
  12. </SBType>
  13. </SBClass>
  14. <SBClass sb_class_id="12430" disporder="-999" name="UK">
  15. <SBType sb_type_id="26463" name="France - Coupe de France" disporder="52">
  16. <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">
  17. <EvDetail br_match_id="8548770"/>
  18. <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"/>
  19. <Teams>
  20. <Team team_id="3411" team_order="0" name="Gazélec Fco Ajaccio" short_name="Gazélec Fco Ajaccio"/>
  21. <Team team_id="172231" team_order="1" name="Sainte Marienne" short_name="Sainte Marienne"/>
  22. </Teams>
  23. </Ev>
  24. </SBType>
  25. </SBClass>
  26. </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.

  1. $xmlData = 'http://feeds-sports.winner.com/odds_feed?key=get_events_for_sport&sport_code=FOOT&on_date=2016-01-02';
  2. $xml = simplexml_load_file($xmlData);
  3. $leagues = $xml->xpath('/Sport/SBClass[@name="United Kingdom"]');
  4. 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 08: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.

    1. $xmlData = 'http://feeds-sports.winner.com/odds_feed?key=get_events_for_sport&sport_code=FOOT&on_date=2016-01-02';
    2. $xml = simplexml_load_file($xmlData);
    3. $sbClassGroups = array();
    4. foreach ($xml->Sport->SBClass as $item) {
    5. if (isset($item->attributes()->name)) {
    6. $sbClassGroups[(string)$item->attributes()->name][] = $item;
    7. }
    8. }
    9. foreach ($sbClassGroups as $sbClassGroup) {
    10. foreach( $sbClassGroup as $sbClass) {
    11. echo "<br><b>" . $sbClass->attributes()->name . "</b><br>";
    12. foreach ($sbClass->SBType as $sbt) {
    13. echo "<br>" . $sbt->attributes()->name . "<br>";
    14. foreach ($sbt->Ev as $ev) {
    15. echo "<i>" . $ev->attributes()->name . "</i><br>";
    16. }
    17. }
    18. }
    19. }
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部