gpt4 book ai didi

php - 在 for 循环中将两个条目粘在一起

转载 作者:行者123 更新时间:2023-12-02 03:01:39 25 4
gpt4 key购买 nike

我有一个来自 JSON 页面的回合和团队列表,如下所示...

ROUND | TEAM-------------6     | D.C. United7     | (blank)8     | New York Red Bulls8     | Los Angeles Galaxy9     | Portland Timbers10    | Chivas USA11    | Seattle Sounders FC11    | Houston Dynamo12    | D.C. United

At the moment, I am echoing it out like shown above, but I would like for any double rounds to show both teams together rather than separately.

Here's an example of what I want to show...

ROUND | TEAM-------------6     | D.C. United7     | (blank)8     | New York Red Bulls & Los Angeles Galaxy9     | Portland Timbers10    | Chivas USA11    | Seattle Sounders FC & Houston Dynamo12    | D.C. United

Here's what I'm using right now.... I'm not sure how to fix it.

//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>";
}

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

最佳答案

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

$rounds = array();

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

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

关于php - 在 for 循环中将两个条目粘在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15738169/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com