如何在PHP中使用Query创建JSON

时间:2015-03-25 11:19:57

标签: php json postgresql

我一直在寻找并尝试使用PHP从postgres表创建下面的JSON的许多不同方法,但是没有用,特别是添加" group"。当有不同的日期时,应该添加组,因此它将按日期创建组,如下例所示。有谁知道我该怎么做?

预期:

"t": [

                {
                    "group": "2015-03-25",
                    "list": [{
                        "t": 1,
                        "titulo": "Pages - Multi-Purpose Admin Template Revolution Begins here!",
                        "to": ["David Nester", "Jane Smith"],
                        "time": "5 min. atrás",
                        "datetime" : "Today at 1:33pm",
                        "from": "David Nester",
                        "icone": 1
                    }, {
                        "t": 2,
                        "titulo": "Your site has some very imaginative animation /movement, especially the Sluggo! ",
                        "to": ["Anne Simons"],
                        "time": "45 min. atrás",
                        "datetime" : "Today at 1:33pm",
                        "from": "Anne Simons",
                        "icone": 2
                    }, {
                        "t": 3,
                        "titulo": "Recently ordered a new pair of soccer cleats from your website on June 21",
                        "to": ["Herald Menster"],
                        "time": "13:33",
                        "datetime" : "Today at 1:33pm",
                        "from": "David Nester",
                        "icone": 1
                    }, {
                        "t": 4,
                        "titulo": "Everything here, Made Just for you :)",
                        "to": ["John Doe"],
                        "time": "11:23",
                        "datetime" : "Today at 11:23am",
                        "from": "David Nester",
                        "icone": 3
                    }, {
                        "t": 5,
                        "titulo": "Simplicity is the ultimate sophistication",
                        "to": ["John Doe", "Anne Simons"],
                        "time": "22:33",
                        "datetime" : "Today at 10:33pm",
                        "from": "David Nester",
                        "icone": 2
                    }]
                }, {
                    "group": "2015-03-24",
                    "list": [{
                        "t": 6,
                        "titulo": "Good design is obvious. Great design is transparent",
                        "to": ["John Doe", "Anne Simons"],
                        "time": "13:33",
                        "datetime" : "Today at 1:33pm",
                        "from": "David Nester",
                        "icone": 1
                    }, {
                        "t": 7,
                        "titulo": "Your site has some very imaginative animation /movement, especially the Sluggo! ",
                        "to": ["Anne Simons"],
                        "time": "45 mins ago",
                        "datetime" : "Today às 13:33",
                        "from": "Anne Simons",
                        "icone": 2
                    }, {
                        "t": 8,
                        "titulo": "Aliquam est tellus, fringilla egestas fermentum quis",
                        "to": ["John Doe", "Anne Simons"],
                        "time": "13:33",
                        "datetime" : "Today at 1:33pm",
                        "from": "David Nester",
                        "icone": 2
                    }, {
                        "t": 9,
                        "titulo": "Aliquam est tellus, fringilla egestas fermentum quis",
                        "to": ["John Doe", "Anne Simons"],
                        "time": "13:33",
                        "datetime" : "Today at 1:33pm",
                        "from": "David Nester",
                        "icone": 1
                    }

当前代码:

使用下面的代码,我得到以下结果,但它永远不会添加不同的" group",只需添加第一个:

echo '{ "t": [';

while ($row = pg_fetch_array($result, null, PGSQL_ASSOC)) {
    $data["group"] = $row["datetime"];
    $data["list"][] = $row; 
};

echo json_encode($data);

echo "]}";

错误的结果:

{
    "t": [
        {
            "group": "2014-09-22",
            "list": [
                {
                    "t": "133640",
                    "titulo": "Some problem",
                    "to": "Erik",
                    "time": "9:30",
                    "datetime": "2014-09-22",
                    "from": "Julian",
                    "icone": "1"
                },
                {
                    "t": "133641",
                    "titulo": "Problems",
                    "to": "Robert",
                    "time": "9:30",
                    "datetime": "2014-09-22",
                    "from": "Julian",
                    "icone": "1"
                }
            ]
        }
    ]
}

4 个答案:

答案 0 :(得分:1)

您可能需要将$data推送到$tickets数组

$tickets = array();
while ($row = pg_fetch_array($result, null, PGSQL_ASSOC)) {
  $data["group"] = $row["datetime"];
  $data["list"] = $row;
  $tickets['tickets'][] = $data;
};

echo json_encode($data);

愿这有帮助!

答案 1 :(得分:1)

试试这个。

<?php
$json = array();    //This is the MAIN array that you'll encode later

$json_tickets = array();        //This is the json tickets, 1st child / parent of all
$json_tickets["list"] = array();

while ($row = pg_fetch_array($result, null, PGSQL_ASSOC)) {

    $new_item = $row;           //Copy $row into a new variable, since you only want to modify $new_item later...

    $new_item["group"] = $new_item["datetime"];
    $json_tickets["list"][] = $new_item;            //keep adding this into a list array
};
$json['tickets'] = $json_tickets;

echo json_encode( $json );

提示:

  • 不要混淆变量。
  • 请勿在代码的这一部分作弊:) - &gt; echo'{“ticket”:[';
  • 所有该表上的表数据将显示在您的JSON结果中,请小心,因为它可能包含敏感数据。可能是一些安全问题
  • 嗯...我还可以在这里提供什么作为你的提示

答案 2 :(得分:1)

创建一个数组,然后使用json_encode php函数

$data = array(
    "group" => "2015-03-24",
    "list" => array(
        "ticket" => 6,
        "titulo" => "Good design is obvious. Great design is transparent",
        "to" => array("John Doe", "Anne Simons"),
        "time" => "13:33",
        "datetime" => "Today at 1:33pm",
        "from"=> "David Nester",
        "icone"=> 1
    )


    );

echo json_encode($data);

这会让你像这样放弃

{"group":"2015-03-24","list":{"ticket":6,"titulo":"Good design is obvious. Great design is transparent","to":["John Doe","Anne Simons"],"time":"13:33","datetime":"Today at 1:33pm","from":"David Nester","icone":1}}

答案 3 :(得分:0)

那是因为你当前的代码:

$data["group"] = $row["datetime"];
$data["list"][] = $row;

用下一次迭代覆盖数组的'group'和'list'成员。

你必须找到解决办法。例如,通过执行以下操作:

$data = array();
while ($row = pg_fetch_array($result, null, PGSQL_ASSOC)) {
   $data[]["group"] = $row["datetime"];
   $data[]["list"][] = $row; 
}

for($i = 0; $i < count($data); $i++)
{
   echo echo json_encode($data[$i]);
}