如何向数组中的特定键添加更多元素?

DocNet

我正在尝试将xml节点转换为数组,我在父match节点内有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++;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用Android中的适配器向GridView添加更多元素

当向其添加更多元素时,插入div块中的表水平重叠

如何在按钮单击上添加更多元素

如何在Mongodb中向数组添加更多字段?

如何将Bootstrap中的Navigation与更多元素重叠?

如何在React Native View中显示更多元素?

从多元素json数组中回显特定元素

如何修改foreach中的数组以处理更多元素,而无需在PHP中进行另一个循环?

由于元素中的空间,将数组元素拆分为更多元素

如何使用AngularJS加载更多元素?

如何使用 javascript 中的提示向数组添加新元素?

如何在PHP中向多维数组添加元素?

如何在C ++中向数组添加元素?

如何向数组键添加新值

如何使用 vanilla JS 向下拉列表动态添加更多元素

不允许将更多元素添加到向量中

如何向作为无状态功能组件的数组元素添加键

如何使用realloc向数组添加更多空间?

在MongoDb中向数组添加新元素

在Java中向数组添加新元素

在Swift中向数组添加元素

如何从包含 17 个元素的列表到更多元素

如何在javascript的文本框中为输入的值创建更多元素?

如何重新编码 r 中行中的重复、三重或更多元素

当元素中包含更多元素时获取文本

向数组添加更多数组

如何在特定位置(由索引给出)向 gsettings 数组添加元素?

如何在python中向xml元素添加多个键和值?

如何从PHP中具有相同键的另一个数组向数组的键添加值?