【发布时间】: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):) -
谢谢。我的方法也有效,但我猜有点奇怪和过于复杂。