Json数据组按团队名称与PHP [英] Json data group by team name with php

查看:50
本文介绍了Json数据组按团队名称与PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在相应的团队名称下显示团队得分 我有以下json数据

I have to display team points under respective team name I have following json data

{
"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

目前,我得到的结果是:

Currently I am getting result like :

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

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

Here is my php code so far :

$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积分

similar code to get team2 points

推荐答案

$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>';
}

这篇关于Json数据组按团队名称与PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆