Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从两个表创建自定义json数组_Php_Mysql_Json_Api_Rest - Fatal编程技术网

Php 从两个表创建自定义json数组

Php 从两个表创建自定义json数组,php,mysql,json,api,rest,Php,Mysql,Json,Api,Rest,我在mysql中有下表。 1) 使用者 我需要json中的以下数据 [ { id:1 username:robert subjects:[ { subjectname:pro. geography , subject teacher:algebra }, { subjectname:pro. vengaskar, subject teacher:pro. re

我在mysql中有下表。 1) 使用者

我需要json中的以下数据

  [  
  {
   id:1
   username:robert      
   subjects:[
   {
       subjectname:pro. geography            ,
       subject teacher:algebra

   },
  {
       subjectname:pro. vengaskar,
       subject teacher:pro. renuka
  }
  ]
  },
  {
   id:2
   username:albert      
   subjects:[
   {
       subjectname:history                          ,
       subject teacher: pro. michale

   },
  {
       subjectname:basic science,
       subject teacher: pro. vengaskar

  }
  ]
  }
  }
我已经开发了json rest api好几次了,但这次是json数组和带有json数组的对象。 我不知道如何迭代内容并获得最终想要的json。 我不会粘贴php代码,因为这对我毫无帮助。
很遗憾,我花了两天时间,但还是在开始时…

用这样一个查询得到结果

SELECT A.userid,A.username, C.subjectname,C.subjectteacher FROM `A` AS users INNER JOIN `UserDetails` AS B on A.userid = b.userid INNER JOIN `subjectdetails` AS C on C.subjectid = B.subjectid
然后在循环中按以下格式操作结果

$arr = array (
        array (
                "id" => "1",
                "username" => "Robert",
                "subjects" => array (
                        array (
                                "subjectname" => "pro. geography",
                                "subject teacher" => "algebra" 
                        ) 
                ) 
        ) 
);

echo json_encode ( $arr );
输出

[
    {
        "id": "1",
        "username": "Robert",
        "subjects": [
            {
                "subjectname": "pro. geography",
                "subject teacher": "algebra"
            }
        ]
    }
]
试试这个

SELECT DISTINCT @pv := usr.userid, CONCAT(  "[", CONVERT( GROUP_CONCAT(DISTINCT  "{\"id\":", usr.userid,  ", \"subjects\":", (
SELECT CONCAT(  "[", CONVERT( GROUP_CONCAT(  "{\"id\":", subjectid,  ", \"subjectname\":\"", subjectname,  "\"}" ) 
USING utf8 ) ,  "]" ) 
FROM subjectdetails
WHERE subjectid
IN (
SELECT subjectid
FROM UserDetails
WHERE userid = @pv)
),  ", \"username\":\"", usr.username,  "\"}" ) 
USING utf8 ) ,  "]" ) AS jsn
FROM User usr
JOIN UserDetails det ON det.userid = usr.userid
LEFT OUTER JOIN subjectdetails sub ON sub.subjectid = det.subjectid
输出:

ID  JSN
1   [{"id":1, "subjects":[{"id":1, "subjectname":"basic science"},{"id":2, "subjectname":"advance mathematics"},{"id":4, "subjectname":"geography"}], "username":"robert"},{"id":2, "subjects":[{"id":1, "subjectname":"basic science"},{"id":2, "subjectname":"advance mathematics"},{"id":4, "subjectname":"geography"}], "username":"albert"}]
具有以下结构的JSN柱:

[
    {
        "id": 1,
        "subjects": [
            {
                "id": 1,
                "subjectname": "basic science"
            },
            {
                "id": 2,
                "subjectname": "advance mathematics"
            },
            {
                "id": 4,
                "subjectname": "geography"
            }
        ],
        "username": "robert"
    },
    {
        "id": 2,
        "subjects": [
            {
                "id": 1,
                "subjectname": "basic science"
            },
            {
                "id": 2,
                "subjectname": "advance mathematics"
            },
            {
                "id": 4,
                "subjectname": "geography"
            }
        ],
        "username": "albert"
    }
]


注意:问题中的JSON结构无效。用于生成有效的json。

您有什么问题?!!在你的JSON格式中,
id:1
是什么?显然这是用户idHi Noor,它的动态来自db字段,因此需要php循环结构。我认为它不会在一个循环内解决。我挂断了循环。实际上我简化了我的实际需求。实际代码很难理解。实际代码有审计表之类的业务逻辑,检查表与审计等相关。因此,我简化了简单的示例,否则实际的sql查询太大。
[
    {
        "id": 1,
        "subjects": [
            {
                "id": 1,
                "subjectname": "basic science"
            },
            {
                "id": 2,
                "subjectname": "advance mathematics"
            },
            {
                "id": 4,
                "subjectname": "geography"
            }
        ],
        "username": "robert"
    },
    {
        "id": 2,
        "subjects": [
            {
                "id": 1,
                "subjectname": "basic science"
            },
            {
                "id": 2,
                "subjectname": "advance mathematics"
            },
            {
                "id": 4,
                "subjectname": "geography"
            }
        ],
        "username": "albert"
    }
]