Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/399.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将mysql转换为json。嵌套对象_Php_Javascript_Mysql_Json_Rest - Fatal编程技术网

使用php将mysql转换为json。嵌套对象

使用php将mysql转换为json。嵌套对象,php,javascript,mysql,json,rest,Php,Javascript,Mysql,Json,Rest,下午好, 我试图将这些结果放入PHP中的数组中,以便将它们编码为json对象并发送给客户端。查询结果如下所示: id name hours cat status 3bf JFK Int 24 pass open 3bf JFK Int 24 std closed 3bf JFK Int 24 exp open 5t6 Ohm CA 18 pass closed 5t6 Ohm CA 18 std closed 5t6 Ohm CA 18 std2

下午好, 我试图将这些结果放入PHP中的数组中,以便将它们编码为json对象并发送给客户端。查询结果如下所示:

   id   name    hours   cat status
3bf JFK Int 24  pass    open
3bf JFK Int 24  std closed
3bf JFK Int 24  exp open
5t6 Ohm CA  18  pass    closed
5t6 Ohm CA  18  std closed
5t6 Ohm CA  18  std2    open
5t6 Ohm CA  18  exp open
...
我希望json对象如下所示:

{ "id": "3bf", "name": "JFK Int", "cats":
    { [ { "cat": "pass", "status": "open" },
        { "cat": "std", "status": "closed" },
        { "cat": "exp", "status": "open" } ] }
{ "id": "5t6", "name": "Ohm CA", "cats":
    { [ { "cat": "pass", "status": "closed" },
        { "cat": "std", "status": "closed" },
        { "cat": "std2", "status": "open" } ],
        { "cat": "exp", "status": "open" } ] }
我已经成功地连接到mysql,并使用json_编码导出,使用平面表,但这部分我不知道如何在PHP中执行。谢谢

这是我的密码。这将返回一个json对象数组,但它是平面的,不是嵌套的:

$SQL = "SELECT id, name, hours, cat, status FROM bwt.vewPortCats";

$result = mysql_query($SQL);

$arr = array();
    while ($row = mysql_fetch_assoc($result)) {
        $arr[] = $row;}

$json = json_encode($arr);

echo $json;
数据本身是从一个结合了表、端口和CAT的角度来看的。

你能做的(对不起,不是我能写的最好的代码……时间、想法和精力都很短;-)是这样的(我希望它仍然能传达这一点):


我还没有时间对它进行测试或三思而后行,但我还是希望它传达了我的想法……

您能给我们看看您的PHP代码吗?从mysql中提取数据有不同的方法,解决方案根据您拥有的内容(例如mysqli、pdo…)而有所不同。您可能要做的是使用foreach迭代“外部”元素,然后再次迭代内部元素(相同的构造)。您有一个多维数组作为输出,然后您可以使用JSON将其转换为JSON,使用group by查询id和名称,并使用group concat获取类别的数组对象。。。提供SQL以便能够通过maraspin为您提供好的评论。对于代码示例和更多内容,请检查我是否已使用我拥有的php代码更新了问题@maraspin,这就是我所想的,但我不知道如何创建一个嵌套数组来链接父母(端口)和孩子(猫)。@bensiu我真的不知道你提到的是什么。我想知道更多关于那件事。那太棒了。我甚至不知道当你有时间、精力和想法时,你能做什么,但这一切都很好。我会接受你的回答,但因为我没有编辑权限,你能在代码上修正3个点吗?$arr[行应该是$arr[$row。另外,为了在json对象上吐出名称,我将$temp=array($row['cat'],$row['status'])改为$temp=array(“cat”=>$row['cat'],“status”=>$row['status']));你说得对@jangeador。为那些拼写错误感到抱歉。很高兴为你找到了解决方案。
$SQL = "SELECT id, name, hours, cat, status FROM bwt.vewPortCats";

$result = mysql_query($SQL);

$arr = array();
    while ($row = mysql_fetch_assoc($result)) {

        // You're going to overwrite these at each iteration, but who cares ;-)
        $arr[$row['id']]['id'] = $row['id'];
        $arr[$row['id']]['name'] = $row['name'];

        // You add the new category
        $temp = array('cat' => $row['cat'], 'status' => $row['status']);

        // New cat is ADDED
        $arr[$row['id']]['cats'][] = $temp;
    }


$base_out = array();

// Kind of dirty, but doesn't hurt much with low number of records
foreach ($arr as $key => $record) {
    // IDs were necessary before, to keep track of ports (by id), 
    // but they bother json now, so we do...
    $base_out[] = $record;
}

$json = json_encode($base_out);

echo $json;