我有一份回合列表&来自JSON页面的团队就像这样......
ROUND | TEAM ------------- 6 | D.C. United 7 | (blank) 8 | New York Red Bulls 8 | Los Angeles Galaxy 9 | Portland Timbers 10 | Chivas USA 11 | Seattle Sounders FC 11 | Houston Dynamo 12 | D.C. United
目前,我正如上面所示回应它,但我希望任何双轮都可以将两支球队展开,而不是分开展示。
以下是我想要展示的一个例子......
ROUND | TEAM ------------- 6 | D.C. United 7 | (blank) 8 | New York Red Bulls & Los Angeles Galaxy 9 | Portland Timbers 10 | Chivas USA 11 | Seattle Sounders FC & Houston Dynamo 12 | D.C. United
这就是我现在正在使用的......我不确定如何修复它。
//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>";
}
我尝试了几种不同的方式,但最终都没有达到我想要的效果。这应该很容易。知道怎么做吗?
答案 0 :(得分:3)
您可以使用中间数组根据轮次编号将团队分组:
$rounds = array();
for($whichround = 0; $whichround < $howmanyrounds; $whichround++)
{
// ...
$rounds[$roundno][] = $team;
}
foreach ($rounds as $roundno => $teams) {
echo $roundno . " " . join(' & ', $teams)."<br>";
}
答案 1 :(得分: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的内容。
答案 2 :(得分: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)
---
)
答案 3 :(得分: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);