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

Php 在for循环中将两个条目粘在一起,php,json,for-loop,Php,Json,For Loop,我有一个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.

我有一个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 轮|队 ------------- 6 |哥伦比亚特区联合 7 |(空白) 8 |纽约红牛队 8 |洛杉矶银河 9 |波特兰木材 10 |芝华士美国 11 |西雅图发声器俱乐部 11 |休斯顿迪纳摩 12 |哥伦比亚特区联合 目前,我正在重复上面所示的内容,但我希望任何两轮比赛都能让两队同时进行,而不是分开进行

这是我想展示的一个例子

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 轮|队 ------------- 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>";
}
//获取页面
$str=文件\u获取\u内容('http://fantasy.mlssoccer.com/web/api/elements/498/');
$jsonarray=json_decode($str,true);
//数一数有多少条
$howmanyrounds=count($jsonarray['fixtures']['all']);
//对于每个条目
对于($whichround=0;$whichround<$howmanyrounds;$whichround++)
{
//这将返回类似“第6轮”的值
$gameweek=$jsonarray['fixtures']['all'][$whichround][1];
//把电话号码删掉
$roundno=intval(substr($gameweek,-(strlen($gameweek)-6));
//这将返回类似“芝华士美国(H)”的值
$反对派=$jsonarray['fixtures']['all'][$whichround][2];
//这将删除实际的团队名称
$team=substr($contactors,0,(strlen($contactors)-4));
echo$roundno.“$team.”
“; }

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

你可以使用一个中间数组根据轮号将团队分组:

$rounds = array();

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

foreach ($rounds as $roundno => $teams) {
    echo $roundno . " " . join(' & ', $teams)."<br>";
}
$rounds=array();
对于($whichround=0;$whichround<$howmanyrounds;$whichround++)
{
// ...
$rounds[$roundno][]=$team;
}
foreach($roundno=>$teams){
echo$roundno.“。加入(“&”,$teams)。”
“; }
尝试将值加入索引数组:

$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=array();
对于($whichround=0;$whichround<$howmanyrounds;$whichround++)
{
//这将返回类似“第6轮”的值
$gameweek=$jsonarray['fixtures']['all'][$whichround][1];
//把电话号码删掉
$roundno=intval(substr($gameweek,-(strlen($gameweek)-6));
//这将返回类似“芝华士美国(H)”的值
$反对派=$jsonarray['fixtures']['all'][$whichround][2];
//这将删除实际的团队名称
$team=substr($contactors,0,(strlen($contactors)-4));
$arrJoined[$roundno]=($arrJoined[$roundno]==null?$team:$arrJoined[$roundno].&'$team);
}

然后只输出$arrJoined的内容。

将其用作中间数组,您应该会很好

$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\u数组的输出

  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)
 ---
)  

我的建议是,只需在代码中添加另一个数组(targetRay),然后将数据推送到该数组中,然后按如下方式打印该数组:

$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);
$targetArray=array()//用于存储值的附加数组
//数一数有多少条
$howmanyrounds=count($jsonarray['fixtures']['all']);
//对于每个条目
对于($whichround=0;$whichround<$howmanyrounds;$whichround++)
{
//这将返回类似“第6轮”的值
$gameweek=$jsonarray['fixtures']['all'][$whichround][1];
//把电话号码删掉
$roundno=intval(substr($gameweek,-(strlen($gameweek)-6));
//这将返回类似“芝华士美国(H)”的值
$反对派=$jsonarray['fixtures']['all'][$whichround][2];
//这将删除实际的团队名称
$team=substr($contactors,0,(strlen($contactors)-4));
//下面是您需要添加的代码。
如果(数组\键\存在($roundno,$targetArray))
$TARGETARAY[$roundno]=$TARGETARAY[$roundno]。“&”。$team;
其他的
$targetArray[$roundno]=$team;
echo$roundno.“$team.”
“; } //这将为您提供数据 印刷(TARGETARAY);
substr($gameweek,-(strlen($gameweek)-6))==substr($gameweek,6)
:)谢谢。我的方法也很有效,但我觉得有点奇怪,太复杂了。谢谢。。这似乎是可行的,但是现在我需要让它忽略所有的空白数组条目,从$ARLANEN(1)到$ARLANEN(5)。我应该能够做到(我想)-谢谢!没问题!此外,没有空白数组项,请尝试打印($arrJoined);:)哦是的,我弄糊涂了。我的意思是,在这种情况下,数字从6开始,根据运行的周数会有所不同,因此我需要解决这个问题,但这也应该是可以管理的。啊,是的-只需使用foreach循环$arrJoined,而不是常规的for循环。您还可以使用我提供的方法在数组中进行免费排序。