2016-03-08 56 views
0

,所以我得到的XML數據徹底API請求和響應這個樣子簡單的XML改變數組的鍵名

<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> 

我simplexml_load_string()加載XML數據,而這個邏輯來處理數據

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()); 

      } 
} 

但是,傾倒$ matchData給出,除了灰髮,數據的陣列,其中統計節點名稱(數組的鍵)被改變爲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" 

        ] 
       ) 
      ) 
     ] 
    ) 

任何幫助表示讚賞

+0

不是這個問題有關。我想要訪問xml/SimpleXML對象中的統計信息節點,但我不能,因爲它的值已更改爲0,如上所述並記錄在上面 – 3ND

+1

SimpleXML不會創建數組。如果您記住* SimpleXML是魔術*,並且*您不能相信它與轉儲函數*,那麼您的頭髮會變得更加灰暗。堅持你想要訪問的內容,並從那裏開始。 – IMSoP

回答

1

您過分依賴數據轉儲,對邏輯上遵循結構並嘗試訪問事物太少。 SimpleXML在請求它們時使用了很多魔術來製作Just Work,並且不以特定格式「包含」數據。

你正在尋找的任務,因爲這(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\n"; 
     } 
    } 
} 
+0

雖然當你傾倒你的方法時,我得到了相同的結果:)必須是前面說過的某種SimpleXML魔術。謝謝 – 3ND

+1

@ 3ND是的,爲了更具體一點,SimpleXML重載了像數組訪問('[0]','['foo']'),屬性訪問(' - > foo')和強制轉換爲字符串'(字符串)$ foo','echo $ foo')讓你的生活更輕鬆。例如,'$ foo-> child-> grandChild'與'$ foo-> child [0] - > grandChild'相同,但在'var_dump'輸出中很難表達。另見https://github.com/IMSoP/simplexml_debug – IMSoP