drzrdc1766788 2016-03-08 10:50
浏览 49
已采纳

simple xml更改数组键名

So i get xml data thorough api requests and responses look like this

 <round round_id="21402" title="Final" start_date="2016-03-06"
     end_date="2016-03-06" scoringsystem="BO3" groups="0" ties="0"
     ordermethod="1" prize_money="6400" prize_currency="usd"
     last_updated="2016-03-06 12:32:41">

     <match match_id="154374" official_start_date="2016-03-06" official_start_time="08:00:00" actual_start_date="" actual_start_time="" winner="B" score_A="1" score_B="2" status="Played" drawposition="1" person_A_id="" person_A_name="" person_A_country="" person_B_id="" person_B_name="" person_B_country="" double_A_id="15014" double_A_country="" person_A1_id="27475" person_A1_name="Liang Chen" person_A1_country="CHN" person_A2_id="29558" person_A2_name="Wang Yafan" person_A2_country="CHN" double_B_id="17263" double_B_country="" person_B1_id="27351" person_B1_name="V. Wongteanchai" person_B1_country="THA" person_B2_id="30339" person_B2_name="Yang Zhaoxuan" person_B2_country="CHN" last_updated="2016-03-06 12:32:41" time_unknown="no">
       <matchstatistics>
         <double double_id="15014" name="Liang Chen / Wang Yafan">
           <statistics aces="1" dbl_faults="3" first_srv_point_won="31"   first_srv_point_total="46" sec_srv_point_won="7" sec_srv="21" break_point_won="6" break_point_total="8" service_games_played="10" first_ret_point_won="13" first_ret_point_total="42" sec_ret_point_won="11" sec_ret_point="19" break_ret_point_won="2" break_ret_total="6" return_games_played="10" total_service_points_won="38" total_service_points_total="67" total_return_points_won="24" total_return_points_total="61" total_points_won="62" total_points="128"/>
         </double>
         <double double_id="17263" name="V. Wongteanchai / Yang Zhaoxuan">
            <statistics aces="1" dbl_faults="2" first_srv_point_won="29" first_srv_point_total="42" sec_srv_point_won="8" sec_srv="19" break_point_won="4" break_point_total="6" service_games_played="10" first_ret_point_won="15" first_ret_point_total="46" sec_ret_point_won="14" sec_ret_point="21" break_ret_point_won="2" break_ret_total="8" return_games_played="10" total_service_points_won="37" total_service_points_total="61" total_return_points_won="29" total_return_points_total="67" total_points_won="66" total_points="128"/>
         </double>
       </matchstatistics>
       <set set_id="352608" score_A="6" score_B="4" tiebreakscore_A="" tiebreakscore_B="" start_date="" start_time="" end_date="" end_time="" last_updated="2016-03-06 12:32:41"/>
       <set set_id="352609" score_A="4" score_B="6" tiebreakscore_A="" tiebreakscore_B="" start_date="" start_time="" end_date="" end_time="" last_updated="2016-03-06 12:32:41"/>
       <set set_id="352610" score_A="7" score_B="10" tiebreakscore_A="" tiebreakscore_B="" start_date="" start_time="" end_date="" end_time="" last_updated="2016-03-06 12:32:41"/>
    </match> 
</round>

I load the xml data with simplexml_load_string(), and this logic to process data

foreach ($matches->xpath('//round') as $roundData) {

       $round = $this->roundsEtl->processRound($roundData, $season);

           foreach ($roundData->match as $matchData) {
                  d($matchData);
                  $game = $this->processGame($season, $round, $matchData);

                   $output->writeln("Game: ".$game->getSlug());

           }
 }

But, dumping $matchData gives, besides gray hair, array of data in which statistics node name (array key) is changed to 0.

public matchstatistics -> SimpleXMLElement (1) (
        public double -> array (2) [
            SimpleXMLElement (2) (
                public @attributes -> array (2) [
                    'double_id' => string (5) "26961"
                    'name' => string (7) "J. Zopp/K. Yoo"
                ]
                public 0 -> SimpleXMLElement (1) (
                    public @attributes -> array (22) [
                        'aces' => string (1) "2"
                        'dbl_faults' => string (1) "1"
                        'first_srv_point_won' => string (2) "17"
                        'first_srv_point_total' => string (2) "32"
                        'sec_srv_point_won' => string (1) "7"

                    ]
                )
            )
            SimpleXMLElement (2) (
                public @attributes -> array (2) [
                    'double_id' => string (5) "11600"
                    'name' => string (8) "T. Kamke/J. Grobe"
                ]
                public statistics -> SimpleXMLElement (1) (
                    public @attributes -> array (22) [
                        'aces' => string (1) "2"
                        'dbl_faults' => string (1) "1"
                        'first_srv_point_won' => string (2) "22"
                        'first_srv_point_total' => string (2) "35"
                        'sec_srv_point_won' => string (2) "13"
                        'sec_srv' => string (2) "19"

                    ]
                )
            )
        ]
    )

Any help is appreciated

  • 写回答

1条回答 默认 最新

  • douwei8672 2016-03-08 16:49
    关注

    You're relying too much on dumping data, and too little on following the structure logically and trying to access things. SimpleXML uses a lot of magic to make things Just Work when you ask for them, and doesn't "contain" the data in a particular format.

    The task you are looking for is as simple as this (live demo):

    foreach ($matches->xpath('//round') as $roundData) {
        foreach ($roundData->match as $matchData) {
            foreach ( $matchData->matchstatistics->double as $double ) {
                echo (string) $double->statistics['first_srv_point_won'], " First Serve Points Won
    ";
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料