所以我通过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 :(得分:1)
您过分依赖于转储数据,而在逻辑上跟踪结构并尝试访问内容的过程太少。当你提出要求时,SimpleXML会使用很多魔法让事情变得简单,并且不会包含&#34;特定格式的数据。
您正在寻找的任务就像这样简单(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";
}
}
}