簡體   English   中英

如何添加更多元素到數組中的特定鍵?

[英]How to add more elements to specific key in array?

我正在嘗試將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;
    }
}

並通過以下方式將此匹配器稱為:

print_r($matcharr);

每個索引/數組只有一個元素,我想從陣容標簽中獲取所有玩家。

因此,print_r將輸出以下內容:

數組([目標] => 2456166 [名稱] =>路易斯·里爾[數字] => 9 [玩家編號] => 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);

在將其存儲在數組中之前,添加一些計數器變量,如下所示

$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++;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM