php - PHP 中的 RestAPI 设计——创建对象的对象而不是对象数组

标签 php arrays json api

尝试创建对象的对象而不是我的代码正在创建对象数组。

我的程序代码

$output['result'] =[];
$query = "SELECT DISTINCT ti.`test_id`, ti.`name`, ti.`subject_id`, ti.`created_timestamp`, ti.`trainer_id`, ti.`class`, ti.`max_marks` , si.`name` as testname, ts.`validity` FROM `test_info` ti, `subject_info` si , `test_schools` ts WHERE ti.`subject_id` = si.`subject_id` AND ts.`test_id` = ti.`test_id` ";
$sth   = $dbh->prepare($query);
if (!$sth->execute()) {
    $dbh = null;
    throw new Exception(-1);
}
$rows = $sth->fetchALL(PDO::FETCH_ASSOC);
foreach($rows as $row){
    $keys = $row['test_id'];
    $output['result'][$keys] = [];  //Creating key 
    array_push($output['result'][$keys],$row); //Provide value to key 
}
echo json_encode($output);

这里我每次在循环中初始化一个数组,因为在 JSON 中将 key 作为 test_id

$output['result'][$keys] = [];

这导致对象数组,那么我如何转换为对象的对象

程序输出

{  
   "result":{  
      "1":[  
         {  
            "test_id":"1",
            "name":"Physics",
            "subject_id":"2",
            "created_timestamp":"2017-03-23 17:55:51",
            "trainer_id":null,
            "class":"8",
            "max_marks":"10",
            "testname":"Physics",
            "validity":"2018-04-14"
         }
      ],
      "2":[  
         {  
            "test_id":"2",
            "name":"phys",
            "subject_id":"2",
            "created_timestamp":"2017-03-24 16:29:33",
            "trainer_id":null,
            "class":"8",
            "max_marks":"5",
            "testname":"Physics",
            "validity":"2017-03-25"
         }
      ]
   }
}

预期输出:

{  
   "result":{  
      "1":{  
         "test_id":"1",
         "name":"Physics",
         "subject_id":"2",
         "created_timestamp":"2017-03-23 17:55:51",
         "trainer_id":null,
         "class":"8",
         "max_marks":"10",
         "testname":"Physics",
         "validity":"2018-04-14"
      },
      "2":{  
         "test_id":"2",
         "name":"phys",
         "subject_id":"2",
         "created_timestamp":"2017-03-24 16:29:33",
         "trainer_id":null,
         "class":"8",
         "max_marks":"5",
         "testname":"Physics",
         "validity":"2017-03-25"
      }
   }
}

最佳答案

不需要使用array_push,一个简单的foreach循环就足够了。在您的代码行 $output['result'][$keys] = []; 中,您正在创建一个数组然后将其插入其中,只有赋值才能帮助您获得预期结果.

将其更改为:

foreach($rows as $row){
    $keys = $row['test_id'];
    $output['result'][$keys] = [];  //Creating key 
    array_push($output['result'][$keys],$row); //Provide value to key 
}

这个:

foreach ($rows as $row)
{
    $keys = $row['test_id'];
    $output['result'][$keys] =$row ;  //Creating key 
}

Try this code snippet here 包含样本输入

关于php - PHP 中的 RestAPI 设计——创建对象的对象而不是对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43715099/

相关文章:

php - MySQL LEFT JOIN 问题与三个 WHERE 语句

java - 为什么这个不会显示?

php - Laravel:从json类型字符串访问键值

json - json.NewEncoder 和 json.NewDecoder 中的序列化/编码

json - 使用 PUP/JQ 将 HTML 转换为 JSON 并将数据提取到变量

php - 图片上传无法正常工作 - Php

php - 在查询中设置字段的条件

php - 为我自己的 PHP 框架制作我自己的参数解析器

java - 如何理解Java中的三维数组

python - 将文件元素读入 3 个不同的数组