【问题标题】:mysql to json using php. Nested objects使用 php 将 mysql 转为 json。嵌套对象
【发布时间】:2011-11-06 22:12:41
【问题描述】:

下午好, 我正在尝试将这些结果放入 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_encode 使用平面表导出,但这部分我不知道如何在 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;

数据本身来自结合了端口和猫表的视图。

【问题讨论】:

  • 你能告诉我们你的PHP代码吗?从 mysql 中提取数据有不同的方法,解决方案因您所拥有的(IE mysqli、pdo ...)而异。您可能会做的是使用 foreach 迭代“外部”元素,然后再次迭代内部元素(相同的构造)。您有一个多维数组作为输出,然后您可以使用 json_encode 将其转换为 JSON
  • 使用 group by 查询 id 和 name,并使用 group_concat 获取类别的数组对象...提供 SQL 以便能够给您示例
  • maraspin 的好评。有关代码示例等,请查看php.net/manual/en/function.json-encode.php
  • 我已经用我拥有的 php 代码更新了这个问题。 @maraspin,这就是我的想法,但我不知道如何创建一个链接父母(端口)和孩子(猫)的嵌套数组。
  • @bensiu 我真的不知道你提到的是什么。我想了解更多。

标签: php javascript mysql json rest


【解决方案1】:

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

$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;

没有时间测试或三思而后行,但我希望它再次传达了这个想法......

【讨论】:

  • 这太棒了。当你有时间、精力和想法时,我什至不知道你能做什么,但这很好用。我会接受作为答案,但由于我没有编辑权限,请您修复代码上 $arr[row 应该是 $arr[$row 的 3 个点。此外,为了让名称吐出 json 对象我更改了 $temp= array($row['cat'], $row['status']); to $temp= array("cat" => $row['cat'], "status" => $row['status']);
  • 你是对的@jangeador。对不起那些错别字。很高兴为您找到解决方案。
猜你喜欢
  • 2014-03-06
  • 2018-10-09
  • 2015-07-21
  • 1970-01-01
  • 1970-01-01
  • 2019-07-04
  • 1970-01-01
  • 1970-01-01
  • 2017-07-27
相关资源
最近更新 更多