使用 php 按团队名称对 Json 数据进行分组

2024-02-18

我必须在各自的团队名称下显示团队积分 我有以下 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>";
			}	
			
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>';
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 php 按团队名称对 Json 数据进行分组 的相关文章

  • 如何解压 PHP/Lumen/Laravel 的 gzip 请求?

    我收到来自第三方的 gzip 编码文本请求 1mb 所以这是有道理的 我的测试路线 router gt post testgzip function Illuminate Http Request request decompressed
  • FPDI/FPDF:水印和打印多页

    我修改了这个堆栈问题 当用户尝试下载文件时在 pdf 文件上应用水印 https stackoverflow com questions 3983432 applying watermarks on pdf files when users
  • 如何使用 php 下载/打印页面的特定部分

    我有一个 HTML 页面如下 Lorem Ipsum is simply dummy text of the printing and typesetting industry Lorem Ipsum has been the indust
  • 扩展蓝图类?

    我想覆盖timestamps 函数中发现Blueprint班级 我怎样才能做到这一点 e g public function up Schema create users function Blueprint table table gt
  • 将“php”作为 shell 脚本执行时的自定义 php.ini 文件

    我在跑php作为 shell 脚本 我不确定 shell脚本 是否正确 该文件以 usr bin php 这很好用 但 MongoDB 类没有正确加载php ini文件 具有extension mongo so 未使用 我该如何使用它tha
  • 如何在原则 2 迁移中删除外键

    我想在原则 2 迁移中删除外键 但没有 dropForeignKeyConstraint 有谁知道怎么丢掉吗 public function down Schema schema table schema gt getTable table
  • preg_match_all 查询仅显示有问题的外部组

    我无法弄清楚如何只显示 preg 查询的外部组级别 我会给你一个例子 preg match all start end input matches 这个输入start1 start2 2end 1end产生这个输出start1 start2
  • 在 Wordpress 站点中进行 AJAX 调用时出现问题

    我在使用 Wordpress 站点功能的 AJAX 部分时遇到了一些问题 该功能接受在表单上输入的邮政编码 使用 PHP 函数来查找邮政编码是否引用特定位置并返回到该位置的永久链接 我的第一个问题是关于我构建的表单 现在我的表单操作是空白的
  • PHP 与 MySQL 查询性能( if 、 函数 )

    我只看到这个artice http www onextrapixel com 2010 06 23 mysql has functions part 5 php vs mysql performance 我需要知道在这种情况下什么是最好的表
  • 覆盖控制器 Symfony 3.4/4.0

    我目前正在尝试覆盖 FOSUserBundle 中的控制器 在新的文档中 https symfony com doc 3 4 bundles override html https symfony com doc 3 4 bundles o
  • 使用 Ajax.Request 将 JSON 从浏览器传递到 PHP 的最佳方法

    您好 我有一个 JSON 对象 它是一个二维数组 我需要使用 Ajax Request 将其传递给 PHP 我知道的唯一方法 现在我使用js函数手动序列化我的数组 并获取以下格式的数据 s 1 d 3 4等 我的问题是 有没有办法更直接 有
  • 在javascript中解析json - 长数字被四舍五入

    我需要解析一个包含长数字的 json 在 java servlet 中生成 问题是长数字被四舍五入 当执行这段代码时 var s x 6855337641038665531 var obj JSON parse s alert obj x
  • SQL 最近日期

    我需要在 php 中获取诸如 2010 04 27 之类的日期作为字符串 并在表中找到最近的 5 个日期 表中的日期保存为日期类型 您可以使用DATEDIFF http dev mysql com doc refman 5 1 en dat
  • 表单提交后如何保留选择字段中的选定值?

    我有一个用于将票证上传到数据库的主页 我有一个选择字段 我想保留用户在提交表单之前选择的值 但它没有发生 这是我选择字段的代码
  • 使用 AsyncTask 传递值

    我一直在努力解决这个问题 但我已经到了不知道该怎么办的地步 我想做的是使用一个类下载文件并将其解析为字符串 然后将该字符串发送到另一个类来解析 JSON 内容 所有部件都可以单独工作 并且我已经单独测试了所有部件 我只是不知道如何将值发送到
  • 使用 Newtonsoft 和 C# 反序列化嵌套 JSON

    我正在尝试解析来自 Rest API 的 Json 响应 我可以获得很好的响应并创建了一些类模型 我正在使用 Newtonsoft 的 Json Net 我的响应中不断收到空值 并且不确定我的模型设置是否正确或缺少某些内容 例如 我想要获取
  • 内部 while 循环不工作

    这是我项目网页上的代码片段 这里我想显示用户选择的类别 然后想显示属于该类别的主题 在那里 用户可以拥有多个类别 这没有问题 我可以在第一个 while 循环中打印所有这些类别 问题是当我尝试打印主题时 结果只显示一行 但每个类别中有更多主
  • ini_set 'session.gc_maxlifetime' 为 1 天

    If I do ini set session gc maxlifetime 86400 这是否意味着用户可以将浏览器留在同一页面 非活动状态 最多 1 天 而不必担心会话被垃圾收集并被注销 如果服务器配置不支持此功能会发生什么 它会给我一
  • Magento - 自定义支付模块

    这是一个非常普遍的问题 但这里是 我正在尝试在 Magento 中创建一个自定义支付模块 我创建了一个 常规 模块 可以连接到 Magento 事件 观察者模型 但是我如何告诉 Magento 将模块视为支付模块 以便它显示在管理后端和结账
  • 使用 powershell 将 XML 转换为特定的 JSON 结构

    需要有关将 xml 转换为特定 json 结构的帮助 XML 看起来像这样

随机推荐