php - 如何在json数组codeigniter中创建json数组

标签 php mysql arrays json codeigniter

我一直在寻找如何从 Mysql 查询在 JSON 对象中创建 JSON 数组的解决方案。需要你们任何人的帮助。

我的mysql源数据:

Table: parent
--------------------------------------
| id | firstname | lastname | rating |
--------------------------------------
|  1 | John      | Doe      | 9.3    |
|  2 | Marry     | Jane     | 8.5    |
|  3 | Paijo     | Masni    | 9.8    |
--------------------------------------

Table: children
------------------------------
| id |  idparent  | name     |
------------------------------
|  1 |  1         | John A   |
|  2 |  1         | John B   |
|  3 |  1         | John C   |
|  4 |  2         | Jane A   |
|  5 |  2         | Jane B   |
|  6 |  3         | Bang Jo  |
|  7 |  3         | Kak Jo   |
------------------------------

我的 MySQL 查询:

选择 p.firstname、p.lastname、p. rating、c.name 作为子项
从父 p 加入子 c 到 p.id = c.idparent

Output:
-------------------------------------------------
| id | firstname | lastname | rating | children |
-------------------------------------------------
|  1 | John      | Doe      | 9.3    | John A   |
|  1 | John      | Doe      | 9.3    | John B   |
|  1 | John      | Doe      | 9.3    | John C   |
|  2 | Marry     | Jane     | 8.5    | Jane A   |
|  2 | Marry     | Jane     | 8.5    | Jane B   |
|  3 | Paijo     | Masni    | 9.8    | Bang Jo  |
|  3 | Paijo     | Masni    | 9.8    | Kak Jo   |
-------------------------------------------------

这是我想要的 JSON 格式的输出:

var profile = [
    {
        "firstname": "John",
        "lastname": "Doe",
        "rating": 9.3,
        "children": [
            "John A",
            "John B",
            "John C"
        ],
        "id": 1
    },
    {
        "firstname": "Marry",
        "lastname": "Jane",
        "rating": 8.5,
        "children": [
            "Jane A",
            "Jane B"
        ],
        "id": 2
    },
    {
        "firstname": "Paijo",
        "lastname": "Masni",
        "rating": 9.8,
        "children": [
            "Bang Jo",
            "Kak Jo"
        ],
        "id": 3
    }
];

我在生成 JSON 时遇到的问题是子项:[],我想要一个带有双引号 ""的数组,在 [] 内用逗号 ',' 分隔。

谢谢。

注意: 目前我正在使用 codeigniter 进行编码。如果您之前在 codeigniter 上遇到过此类问题,我期待您的分享。

最佳答案

感谢大家的宝贵回复。 我刚刚得到了解决方案。

在使用 mysql 中的任何可用函数(我使用过 json_extract() 和 group_concat() )搜索任何解决方案后,但它没有提供我想要的最佳格式。

受到上述答案的启发, 我在 Codeigniter 中编写了这样的代码,效果完美:

$parent   = array();
$children = array();
$profile  = array();

$parent_query = $this->db->query("select firstname, lastname, rating, id from parent")->result_array();

for($i = 0; $i < count($parent_query); $i++)
{
   $children_query = $this->db->query("select name from children where idparent = '$parent_query[$i]['id']'")->result_array();

   $parent[$i]['firstname'] = $parent_query[$i]['firstname'];
   $parent[$i]['lastname']  = $parent_query[$i]['lastname'];
   $parent[$i]['rating']    = $parent_query[$i]['rating'];

   for($j = 0; $j < count($children_query); $j++)
   {
      $children[] = $children_query[$j]['name'];
   }

   $parent[$i]['children']  = $children;
}

$profile = json_encode($parent);

给出的结果:

[  
   {  
      "firstname":"John",
      "lastname":"Doe",
      "rating":9.3,
      "children":[  
         "John A",
         "John B",
         "John C"
      ],
      "id":1
   },
   {  
      "firstname":"Marry",
      "lastname":"Jane",
      "rating":8.5,
      "children":[  
         "Jane A",
         "Jane B"
      ],
      "id":2
   },
   {  
      "firstname":"Paijo",
      "lastname":"Masni",
      "rating":9.8,
      "children":[  
         "Bang Jo",
         "Kak Jo"
      ],
      "id":3
   }
]

关于php - 如何在json数组codeigniter中创建json数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47308598/

相关文章:

php - 无法从 codeigniter 中的 URL 中删除 "Index.php"

mysql - 提高Mysql查询性能(使用explain类型索引)

python - 数据库错误 python manage.pysyncdb

javascript - Jquery 映射数组键和值

php - 无法为 php 安装 id3 - 什么是好的选择?

php - 否则 mysql 查询占用前 20 个日期

Java - 将数据从txt文件传输到数组中

javascript - 使用数组对对象进行排序并将值存储在另一个数组中

php - 与 ReactPhp 并行运行代码

mysql - 如何使用MySQL通过索引获取列名?