[英]Json data group by team name with php
I have to display team points under respective team name I have following json data 我必须在各自的团队名称下显示团队得分,我有以下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} ] }, }
Now I want result like : 现在我想要这样的结果:
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
Here is my php code so far : 到目前为止,这是我的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>"; }
$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>';
}
Use json_decode() with true
as second parameter it gives you an associative array and it will convert the JSON object
into a PHP object
.It will make your life easier. 将json_decode()与
true
作为第二个参数使用,它将为您提供一个关联数组,并将JSON object
转换为PHP object
这将使您的生活更轻松。
Try this : 尝试这个 :
$jsonObj = json_decode($json,true);
$inningsPoint = [];
foreach ($jsonObj as $teamData) {
if(!empty($teamData[team][name])) {
echo $teamData[team][name].'<br>';
$inningsPoint = [];
}
foreach ($teamData[innings] as $inningsData) {
$inningsPoint[] = "$inningsData[points] / $inningsData[wickets]";
}
echo implode(' & ', $inningsPoint);
}
<?php
function showteams_results($k1,$points){
switch ($k1) {
case 'team1':
foreach ($points as $key => $value) {
if($key=="team1")
{
echo ucfirst($key)."<br>";
foreach ($value as $k => $v) {
echo "Innings".$v."<br>";
}
}
}
break;
case 'team2':
foreach ($points as $key => $value) {
if($key=="team2")
{
echo ucfirst($key)."<br>";
foreach ($value as $k => $v) {
echo "Innings".$v."<br>";
}
}
}
break;
default:
echo "Team no points!";
break;
}
}
$points=array();
$jsonObj = json_decode($json,true);
$inningsPoint = [];
foreach ($jsonObj as $k1 => $teamData) {
//extracting points of team1---------->
if($k1=="team1"){
//showteams_results($k,$jsonObj);
foreach ($teamData as $k => $v) {
if($k=="innings")
{
foreach ($v as $k => $vf) {
foreach ($vf as $k => $vk) {
if($k=="points")
$points["team1"][]=$vk;
}
}
}
}
//sending data teams
showteams_results($k1,$points);
}
//extracting points of team2---------->
if($k1=="team2"){
//showteams_results($k,$jsonObj);
foreach ($teamData as $k => $v) {
if($k=="innings")
{
foreach ($v as $k => $vf) {
foreach ($vf as $k => $vk) {
if($k=="points")
$points["team2"][]=$vk;
}
}
}
}
//sending data teams
showteams_results($k1,$points);
}
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.