Json数据组按团队名称与PHP

我必须在各自的球队名称显示球队积分
我有以下JSON数据

{
"id":319231,
"innings":[
	{"id":967766},
	{"id":967767},
	{"id":967768},
	{"id":967769}
],
"team1":{
	"team":{"name":"Minor Counties","id":115104,"club":{"name":"Minor Counties","id":98110}},
	"innings":[
		{"id":967766,"points":253,"wickets":10,"overs":86,"balls":4},
		{"id":967768,"points":190,"wickets":5,"overs":61,"balls":0}
	]
},
"team2":{
	"team":{"name":"Major Counties","id":93648,"club":{"name":"Major Counties","id":35487}},
	"innings":[
		{"id":967767,"points":229,"wickets":10,"overs":67,"balls":4},
		{"id":967769,"points":64,"wickets":4,"overs":23,"balls":2}
	]
},
}

现在我想要这样的结果:

Minor Counties             Major Counties
253/10 &  190/5            229/10 & 64/4

目前,我得到的结果是:

Minor Counties       Minor Counties    Major Counties   Major Counties
  253/10                190/5            229/10             64/4

到目前为止,这是我的PHP代码

$team1 = $read_json->team->team1->name;
$team2 = $read_json->team->team2->name;
foreach($read_json->team1->innings as $team1Innings){
				$points = $team1Innings->points;
				$wickets = $team1Innings->wickets;
				$overs = $team1Innings->overs;
				$balls = $team1Innings->balls;				
                echo "<div class=\"score-total\"><span class=\"score-team\">$team1</span>$points/$wickets<span class=\"score-overs\">$overs.$balls overs</span></div>";
			}	
			

类似的代码获得team2积分

解决方法:

$json = '{
   "id":319231,
   "innings":[
      {
         "id":967766
      },
      {
         "id":967767
      },
      {
         "id":967768
      },
      {
         "id":967769
      }
   ],
   "team1":{
      "team":{
         "name":"Minor Counties",
         "id":115104,
         "club":{
            "name":"Minor Counties",
            "id":98110
         }
      },
      "innings":[
         {
            "id":967766,
            "points":253,
            "wickets":10,
            "overs":86,
            "balls":4
         },
         {
            "id":967768,
            "points":190,
            "wickets":5,
            "overs":61,
            "balls":0
         }
      ]
   },
   "team2":{
      "team":{
         "name":"Major Counties",
         "id":93648,
         "club":{
            "name":"Major Counties",
            "id":35487
         }
      },
      "innings":[
         {
            "id":967767,
            "points":229,
            "wickets":10,
            "overs":67,
            "balls":4
         },
         {
            "id":967769,
            "points":64,
            "wickets":4,
            "overs":23,
            "balls":2
         }
      ]
   }
}';

$items = json_decode($json);
unset($items->id);
unset($items->innings);
foreach ($items as $item) {
    echo "<b>{$item->team->name}</b>";
    $innings = [];
    foreach ($item->innings as $inning) {
        $innings[] = "{$inning->points} / {$inning->wickets}";
    }
    echo '<br>';
    echo implode(' & ', $innings);
    echo '<br>';
}

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...