【问题标题】:Sticking together double entries in a for loop在 for 循环中将两个条目粘在一起
【发布时间】:2013-03-22 05:11:10
【问题描述】:

我有一个来自 JSON 页面的回合和团队列表......

圆形 |团队 ------------- 6 |华盛顿联合 7 | (空白的) 8 |纽约红牛队 8 |洛杉矶银河 9 |波特兰木材 10 |美国芝华士 11 |西雅图海湾人队 11 |休斯顿迪纳摩 12 |华盛顿联合

目前,我正在回应如上所示,但我希望任何双轮比赛都将两支球队一起展示,而不是分开展示。

这是我要展示的示例...

圆形 |团队 ------------- 6 |华盛顿联合 7 | (空白的) 8 |纽约红牛队和洛杉矶银河队 9 |波特兰木材 10 |美国芝华士 11 |西雅图海湾人队和休斯顿迪纳摩队 12 |华盛顿联合

这是我现在正在使用的......我不知道如何解决它。

//get the page 
$str = file_get_contents('http://fantasy.mlssoccer.com/web/api/elements/498/');
$jsonarray = json_decode($str, true);

//count how many entries
$howmanyrounds = count($jsonarray['fixtures']['all']);

//for each entry
for($whichround = 0; $whichround < $howmanyrounds; $whichround++)
{
    //this returns a value like 'Round 6'
    $gameweek = $jsonarray['fixtures']['all'][$whichround][1];
    //Cut out just the number
    $roundno = intval(substr($gameweek, -(strlen($gameweek)-6)));

    //this returns a value like 'Chivas USA (H)'
    $opponents = $jsonarray['fixtures']['all'][$whichround][2];
    //This cuts out the actual team name
    $team = substr($opponents, 0, (strlen($opponents)-4));

    echo $roundno." ".$team."<br>";
}

我尝试了几种不同的方法,但最终都无法达到我想要的效果。这应该很容易。知道怎么做吗?

【问题讨论】:

  • substr($gameweek, -(strlen($gameweek)-6)) == substr($gameweek, 6) :)
  • 谢谢。我的方法也有效,但我猜有点奇怪和过于复杂。

标签: php json for-loop


【解决方案1】:

您可以使用中间数组根据轮数将团队分组:

$rounds = array();

for($whichround = 0; $whichround < $howmanyrounds; $whichround++)
{
    // ...
    $rounds[$roundno][] = $team;
}

foreach ($rounds as $roundno => $teams) {
    echo $roundno . " " . join(' & ', $teams)."<br>";
}

【讨论】:

    【解决方案2】:

    尝试将值加入索引数组:

    $arrJoined = array();
    
    for($whichround = 0; $whichround < $howmanyrounds; $whichround++)
    {
        //this returns a value like 'Round 6'
        $gameweek = $jsonarray['fixtures']['all'][$whichround][1];
        //Cut out just the number
        $roundno = intval(substr($gameweek, -(strlen($gameweek)-6)));
    
        //this returns a value like 'Chivas USA (H)'
        $opponents = $jsonarray['fixtures']['all'][$whichround][2];
        //This cuts out the actual team name
        $team = substr($opponents, 0, (strlen($opponents)-4));
    
        $arrJoined[$roundno] = ($arrJoined[$roundno] == null ? $team : $arrJoined[$roundno].' & '.$team);
    }
    

    然后只输出你的 $arrJoined 的内容。

    【讨论】:

    • 谢谢.. 这似乎有效,但现在我需要让它忽略从 $arrJoined[1] 到 $arrJoined[5] 的所有空白数组条目。我应该能够做到(我认为) - 谢谢!
    • 没问题!也没有空白数组条目,请尝试 print_r($arrJoined); :)
    • 哦..是的,我把它弄糊涂了。我的意思是,在这种情况下,数字从 6 开始,并且会根据运行的周数而有所不同,所以我需要解决它,但这也应该是可以管理的。
    • 啊,是的 - 只需使用 foreach 循环 $arrJoined 而不是常规的 for 循环。您还可以使用我提供的方法在数组中免费排序。
    【解决方案3】:

    用它作为中间数组,你应该很好

    $str = file_get_contents('http://fantasy.mlssoccer.com/web/api/elements/498/');
                $array = json_decode($str, true);
                $temp = $array['fixtures']['all'];
                $req_array = array();
                foreach($temp as $value)
                {
                    list($dummy, $round)  = explode(" ", $value[1]);
                               $value[2] = str_replace('-','',$value[2]);
                    if(isset($req_array["Round $round"]) 
                       && ($req_array["Round $round"] != '') 
                       )
                    {
                        $req_array["Round $round"] = $req_array["Round $round"]."&".$value[2];
                    }
                    else if($value[2] != '-')
                    {
                        $req_array["Round $round"] = $value[2];
                    }
                }
    

    输出来自$req_array

      Array
       (
        [Round 6] => D.C. United (H)
    [Round 7] => 
    [Round 8] => New York Red Bulls (A)&Los Angeles Galaxy (A)
    [Round 9] => Portland Timbers (H)
    [Round 10] => Chivas USA (H)
    [Round 11] => Seattle Sounders FC (H)&Houston Dynamo (A)
    [Round 12] => D.C. United (A)
     ---
    )  
    

    【讨论】:

      【解决方案4】:

      我的建议是,您只需编写另一个数组 (targetAray),然后将数据推送到该数组中,然后打印该数组,如下所示:

      $targetArray = array();   //additional array to store values
      //count how many entries
      $howmanyrounds = count($jsonarray['fixtures']['all']);
      
      //for each entry
      for($whichround = 0; $whichround < $howmanyrounds; $whichround++)
      {
          //this returns a value like 'Round 6'
          $gameweek = $jsonarray['fixtures']['all'][$whichround][1];
      
          //Cut out just the number
          $roundno = intval(substr($gameweek, -(strlen($gameweek)-6)));
      
          //this returns a value like 'Chivas USA (H)'
          $opponents = $jsonarray['fixtures']['all'][$whichround][2];
          //This cuts out the actual team name
          $team = substr($opponents, 0, (strlen($opponents)-4));
      
         //below code you need to add. 
         if(array_key_exists($roundno, $targetArray))
           $targetArray[$roundno] = $targetArray[$roundno]. "&" .$team;
         else
          $targetArray[$roundno] = $team;
          echo $roundno." ".$team."<br>";
      }
      //this will give you your data
      print_r($targetArray);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-04
        • 2017-08-26
        • 1970-01-01
        • 2012-04-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多