我正在尝试将xml节点转换为数组,我在父匹配节点中有3个独立的节点,所以我需要将它们全部转换为数组。
当我尝试这段代码时
foreach($lineup->away->player as $player){
$awaysquad .= $player->attributes()->name . '; ';
$matcharr['name'] = (string)$player->attributes()->name;
$matcharr['number'] = (int)$player->attributes()->number;
$matcharr['playerid'] = (int)$player->attributes()->id;
$yellowcount = count(explode(" ",$player->attributes()->booking));
if(substr_count($player->attributes()->booking,"YC") == 1){
$matcharr['yellow'] = (int)$player->attributes()->id;
}
elseif((substr_count($player->attributes()->booking,"RC")==1)
or ($yellowcount == 3)){
$matcharr['red'] = (int)$player->attributes()->id;
}
}
并通过以下方式调用此matcharr:
print_r($matcharr);
每个索引/数组只有最后一个元素,我希望从阵容标记中获取所有玩家。
所以print_r打印出来:
数组([goal] => 2456166 [name] =>LuísLeal[数字] => 9 [playerid] => 2474225 [黄色] => 2486288 [subin] => 2353344 [subout] => 0 [分钟] => NA)
每个比赛标签,但我需要为每场比赛获得22名球员。
以下是更详细的代码:
foreach($week->match as $match){
$matcharr = array();
//var_dump($match);
$fixid = 0;
if($match->attributes()->id == 0 || $match->attributes()->id == ''){
if($match->attributes()->alternate_id == 0 || $match->attributes()->id == ''){
$fixid = $match->attributes()->alternate_id_2;
}
else{
$fixid = $match->attributes()->alternate_id;
}
}
else{
$fixid = $match->attributes()->id;
}
$dbdate = date('Y-m-d',strtotime($match->attributes()->date));
$dbtime = date('H:i:s', strtotime($match->attributes()->time));
//if($dbdate == date('Y-m-d')){
echo $dbdate . ' ' . date("Y-m-d");
$datetime = date('Y-m-d H:i:s',strtotime($dbdate . $dbtime));
$fcountry = $this->filterCountries($results->attributes()->country);
$stadium = $match->attributes()->venue;
$city = $match->attributes()->venue_city;
//echo $fcountry;
//$home = $match->home->attributes()->name;
//$away = $match->away->attributes()->name;
$home = '';
$away = '';
$homeid = 0;
$awayid = 0;
$hgoals = 0;
$agoals = 0;
if($match->home){
$home = $this->getMap($fcountry,$match->home->attributes()->name);
$away = $this->getMap($fcountry,$match->away->attributes()->name);
$homeid = $this->filterTeams($fcountry,$match->home->attributes()->id);
$awayid = $this->filterTeams($fcountry,$match->away->attributes()->id);
$hgoals = $match->home->attributes()->score;
$agoals = $match->away->attributes()->score;
}
$eventname = $home . ' - ' . $away;
$halftimehomegoals = 0;
$halftimeawaygoals = 0;
if($match->halftime->attributes()->score != NULL){
$halftime = explode("-",$match->halftime->attributes()->score);
$halftimehomegoals = (int)$halftime[0];
if(array_key_exists(1,$halftime)){
$halftimeawaygoals = (int)$halftime[1];
}
}
$homescore = '';
$awayscore = '';
if($match->goals->goal != NULL){
foreach($match->goals->goal as $goal){
if($goal->attributes()->team == 'home'){
$homescore .= $goal->attributes()->minute.': '.
$goal->attributes()->player.'; ';
$matcharr['goal'] = (int)$goal->attributes()->playerid;
}
elseif($goal->attributes()->team == 'away'){
$awayscore .= $goal->attributes()->player.'; ';
$matcharr['goal'] = (int)$goal->attributes()->playerid;
}
}
}
$homesquad = '';
$awaysquad = '';
if($match->lineups != NULL){
foreach($match->lineups as $lineup){
if($lineup->home->player != NULL){
foreach($lineup->home->player as $player){
$homesquad .= $player->attributes()->name . '; ';
$matcharr['name'] = (string)$player->attributes()->name;
$matcharr['number'] = (int)$player->attributes()->number;
$matcharr['playerid'] = (int)$player->attributes()->id;
$yellowcount = count(explode(" ",$player->attributes()->booking));
if(substr_count($player->attributes()->booking,"YC") == 1){
$matcharr['yellow'] = (int)$player->attributes()->id;
}
elseif((substr_count($player->attributes()->booking,"RC")==1)
or ($yellowcount == 3)){
$matcharr['red'] = (int)$player->attributes()->id;
}
}
}
if($lineup->away->player != NULL){
foreach($lineup->away->player as $player){
$awaysquad .= $player->attributes()->name . '; ';
$matcharr['name'] = (string)$player->attributes()->name;
$matcharr['number'] = (int)$player->attributes()->number;
$matcharr['playerid'] = (int)$player->attributes()->id;
$yellowcount = count(explode(" ",$player->attributes()->booking));
if(substr_count($player->attributes()->booking,"YC") == 1){
$matcharr['yellow'] = (int)$player->attributes()->id;
}
elseif((substr_count($player->attributes()->booking,"RC")==1)
or ($yellowcount == 3)){
$matcharr['red'] = (int)$player->attributes()->id;
}
}
}
}
}
$homesub = '';
$awaysub = '';
if($match->substitutions != NULL){
foreach($match->substitutions as $subs){
if($subs->home->substitution != NULL){
foreach($subs->home->substitution as $sub){
$homesub .= $sub->attributes()->minute."' in: ".
$sub->attributes()->player_in_name . '; ' . ' out: ' .
$sub->attributes()->player_out_name . '; ';
$matcharr['subin'] = (int)$sub->attributes()->player_in_id;
$matcharr['subout'] = (int)$sub->attributes()->player_out_id;
$matcharr['minute'] = (string)$sub->attributes()->minute;
}
}
if($subs->away->substitution != NULL){
foreach($subs->away->substitution as $sub){
$awaysub .= $sub->attributes()->minute."' in: ".
$sub->attributes()->player_in_name . '; ' .
$sub->attributes()->player_out_name . '; ';
$matcharr['subin'] = (int)$sub->attributes()->player_in_id;
$matcharr['subout'] = (int)$sub->attributes()->player_out_id;
$matcharr['minute'] = (string)$sub->attributes()->minute;
}
}
}
}
echo $leaguename . ' ' . $leagueid . ' ' . $fixid . ' ' . $eventname.'<br>';
print_r($matcharr);
答案 0 :(得分:1)
添加一些计数器变量,然后将其存储在数组中,如下所示
$element_count = 0;//Counter variable
foreach ($lineup->away->player as $player) {
$awaysquad .= $player->attributes()->name . '; ';
$matcharr[$element_count]['name'] = (string)$player->attributes()->name;
$matcharr[$element_count]['number'] = (int)$player->attributes()->number;
$matcharr[$element_count]['playerid'] = (int)$player->attributes()->id;
$yellowcount = count(explode(" ", $player->attributes()->booking));
if (substr_count($player->attributes()->booking, "YC") == 1) {
$matcharr[$element_count]['yellow'] = (int)$player->attributes()->id;
}
elseif ((substr_count($player->attributes()->booking, "RC") == 1) or ($yellowcount == 3)) {
$matcharr[$element_count]['red'] = (int)$player->attributes()->id;
}
$element_count++;
}