2

我有一个来自 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>";
}

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

4

4 回答 4

3

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

$rounds = array();

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

foreach ($rounds as $roundno => $teams) {
    echo $roundno . " " . join(' & ', $teams)."<br>";
}
于 2013-04-01T05:46:20.590 回答
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 的内容。

于 2013-04-01T05:47:56.410 回答
1

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

$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)
 ---
)  
于 2013-04-01T06:01:30.627 回答
1

我的建议是,您的代码中只是另一个数组(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);
于 2013-04-01T06:22:55.257 回答