Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.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数组codeigniter中创建json数组_Php_Mysql_Arrays_Json_Codeigniter - Fatal编程技术网

Php 如何在json数组codeigniter中创建json数组

Php 如何在json数组codeigniter中创建json数组,php,mysql,arrays,json,codeigniter,Php,Mysql,Arrays,Json,Codeigniter,我一直在寻找如何从Mysql查询在JSON对象中创建JSON数组的解决方案。需要你们中任何一个人的帮助 mysql中的我的源数据: Table: parent -------------------------------------- | id | firstname | lastname | rating | -------------------------------------- | 1 | John | Doe | 9.3 | | 2 | Marry

我一直在寻找如何从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在p.id=c.idparent上连接子级c

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上遇到过这样的问题,我期待着与你分享

$people = array();
$person = array();

$person['firstname'] = "John";
$person['lastname'] = "Doe";
$person['rating'] = 9.3;
$person['children'] = array('John A', 'John B', 'John C');
$person['id'] = 1;

$people[] = $person;
$person = array();

$person['firstname'] = "Marry";
$person['lastname'] = "Jane";
$person['rating'] = 8.5;
$person['children'] = array('JaneA', 'JaneB');
$person['id'] = 2;

$people[] = $person;

$profile = json_encode($people);

echo $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":["JaneA","JaneB"],"id":2}]
提供以下输出:

[{"firstname":"John","lastname":"Doe","rating":9.3,"children":["John A","John B","John C"],"id":1},
{"firstname":"Marry","lastname":"Jane","rating":8.5,"children":["JaneA","JaneB"],"id":2}]

我用的是NEIL的数组结构

在codeigniter中,在末尾使用以下代码进行编码

$this->output->set_content_type('application/json');
$this->output->set_输出(json_编码($your_数组))

它将输出以下内容:

“[{firstname\”:“John\”,“lastname\”:“Doe\”,“rating\”:9.3,“children\”:[“John A\”,“John B\”,“John C\”],“id\”:1},{“firstname\”:“Marry\”,“lastname\”:“Jane\”,“rating\”,“rating\”:8.5,““children\”:“children\”:“JaneA\”,“JaneB\”,“id\”,“id\”,“:[/code>


在AJAX返回中,使用$.parseJSON(数据)获得所需的结果。

我使用的是NEIL的数组结构

在codeigniter中,在末尾使用以下代码进行编码

$this->output->set_content_type('application/json');
$this->output->set_输出(json_编码($your_数组))

它将输出以下内容:

“[{firstname\”:“John\”,“lastname\”:“Doe\”,“rating\”:9.3,“children\”:[“John A\”,“John B\”,“John C\”],“id\”:1},{“firstname\”:“Marry\”,“lastname\”:“Jane\”,“rating\”,“rating\”:8.5,““children\”:“children\”:“JaneA\”,“JaneB\”,“id\”,“id\”,“:[/code>


在AJAX return中,使用$.parseJSON(数据)获得所需的结果。

谢谢大家的宝贵回复。 我刚找到解决办法

在使用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);

谢谢大家的宝贵回复。 我刚找到解决办法

在使用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);

您的数据源是什么?我的数据源来自Mysql数据库。目前,我在codeigniter中使用json_encode()来生成json数组,但它的格式非常通用,json数组中没有数组。您的数据源是什么?我的数据源来自Mysql数据库。目前,我在codeigniter中使用json_encode()来生成json数组,但结果是非常通用的格式,json数组中没有数组。谢谢,伙计。我从你的回答中得到了答案。谢谢,伙计。我从你的回答中得到了答案。