【问题标题】:Complex nested JSON Array from PhP Coding来自 PhP 编码的复杂嵌套 JSON 数组
【发布时间】:2015-09-14 18:43:42
【问题描述】:

我已经从数据库中创建了一个 JSON 文件,它有两个表 semone,属性为 id、semester、cname 和表课程,属性为 coname 和 credit。

我在 php 中编写的代码如下。

main.php

<?php
    $user = "root";
    $password = "";
    $database = "scheduler";

    $con = mysqli_connect("localhost", $user, $password, $database) or die ("Unable to connect"); 


    $query = "SELECT semone.userid AS sbuid, semone.semester AS semester, 
                      semone.cname AS name, courses.credit AS value, 
                      courses.progskill AS skill
              FROM semone
              INNER JOIN courses ON semone.cname = courses.coname" ;

    $result = mysqli_query($con,$query)or die ("Unable to connect");

    $info = array();
    $test = array();

    while ($row = $result->fetch_array(MYSQLI_ASSOC)) {

        $row['xyz'] = array(
                            'name'=> $row['name'],
                            'value'=> $row['value']    
                           );

        $info[$row['semester']]['children'][]= $row['xyz'];

        $data = json_encode(array('id' => $row['sbuid'], 'children' => $info));
    }
    echo $data;
?>

我想得到如下代码所示的 JSON 文件,但我得到了类似的东西。

output.json

  {"id":"12345", 
        "children":

        {"first":
        {"children":
        [{"name":"CSE101","value":"100"},
       {"name":"CSE102","value":"100"}]},

        "second":
        {"children":
      [{"name":"CSE103","value":"50"}, 
      {"name":"CSE104","value":"100"},
      {"name":"CSE105","value":"100"}]},

        "third":
        {"children":
        [{"name":"CSE106","value":"50"}]}
         }}

但这正是我所期待的。

expected.json

{
 "id": 12345,
 "children":
  [{
     "semester": "first",
     "children": 
     [{
     "name": "C101","value": 100},
      { "name": "C102","value": 100}]
    }, 
    {
    "semester": "second",
      "children": 
      [{
     "name": "C103", "value": 50}, 
     {"name": "C104","value": 100}, 
     {"name": "C105","value": 100}]
}, 
{
 "semester": "third",
 "children":  
  [{"name": "C106","value": 50}]
 }
}

【问题讨论】:

  • @RiggsFolly 请看一下。我需要它但无法解决它。
  • 您离它很近,首先我假设您将获取多个用户的数据,因为您对查询没有添加任何限制。但是你没有代码来处理一个以上的学生或每个学生超过一个学期。所以不是免费的编码资源您基本上是在要求某人编写您的代码,但甚至没有正确解释您的要求或数据库结构。 Trapper 会很生气的
  • @RiggsFolly 我明白了你的意思,基本上这将是最后的事情,但由于我是编程新手,所以我想一步一步地进行。可能是先获取单个学生的数据,然后再获取多个学生的数据。可能是我走错了方向。抱歉不知道。

标签: php mysql arrays json


【解决方案1】:

将您的 while 循环替换为:

$lastId = null;
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {

    $row['xyz'] = array(
                        'name'=> $row['name'],
                        'value'=> $row['value']    
                       );

    $info[$row['semester']]['semester'] = $row['semester'];
    $info[$row['semester']]['children'][]= $row['xyz'];

    $lastId = $row['sbuid'];
}

// do not call json_encode on each iteration of the loop
$data = json_encode(array('id' => $lastId, 'children' => array_values($info)));

解释

您希望 $row['semester'] 成为 children 对象的字段,但您将此值设置为键。您需要使用semester 键显式设置此值(与深层children 数组平行)并在编码前使用array_values 删除键(关联数组编码为对象,而数字数组编码为数组)。

另外,我不知道在 JSON 中只保存最后一个 $row['sbuid'],也许你想在这里做一些不同的事情?

阅读更多关于 PHP 的 arraysjson encode

【讨论】:

  • ID 的作用是将不同学生的 ID 分开,因为 id 位于 while 循环之外,因此无法从数据库中获取值并返回“null”值。为了达到这个目的,我认为应该在while循环中使用“id”,然后我们才能从数据库中获取它并返回确切的值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-16
  • 1970-01-01
  • 2013-04-18
  • 2022-11-11
  • 2018-09-27
相关资源
最近更新 更多