如何使用 group by 子句 php 的结果创建自定义 json 对象
How to make custom json object with the result of a group by clause, php
我正在尝试从 php 脚本中获取 json 对象。到目前为止我已经做到了。
<?php
$connection=pg_connect("host=localhost port=5432 dbname=postgres user=postgres password=root") or die("Can't connect to database".pg_last_error());
$result = pg_query('SELECT * FROM playground');
$rows = array();
while($r = pg_fetch_assoc($result)) {
$rows[] = $r;
}
print json_encode(array_values(pg_fetch_all($result)));
?>
这个returns下面的对象
[{"name":"slide","color":"blue","location":"south"},{"name":"slide","color":"green","location":"north"},{"name":"dont","color":"red","location":"west"}]
但是,我正在尝试将它们存储在更像....
[
{"name":"slide","values": [ "color" : ["blue","green"], "location": ["south", "north"] ]},
{"name":"dont","values" : ["color" : ["red"],"location" : ["west"] }
]
基本上,我想按父字段值作为键对公共组进行分组,并将它们的值作为 json 对象的值。
但是我做不到。如果有人可以提供帮助,将不胜感激。
谢谢。
您可以循环遍历$result
并自己创建所需的数组结构:
while($r = pg_fetch_assoc($result)) {
if (!array_key_exists($r['name'], $rows)) {
$rows[$r['name']] = array('values' => array( 'color' => array(), 'location' => array() ));
}
if (!in_array($r['color'], $rows[$r['name']]['values']['color'])) {
$rows[$r['name']]['values']['color'][] = $r['color'];
}
if (!in_array($r['location'], $rows[$r['name']]['values']['location'])) {
$rows[$r['name']]['values']['location'][] = $r['location'];
}
}
echo json_encode($rows);
这将在表格
上生成 JSON
{
"slide": {
"values": {
"color": [
"blue",
"green"
],
"location": [
"south",
"north"
]
}
},
"dont": {
"values": {
"color": [
"red"
],
"location": [
"west"
]
}
}
}
我正在尝试从 php 脚本中获取 json 对象。到目前为止我已经做到了。
<?php
$connection=pg_connect("host=localhost port=5432 dbname=postgres user=postgres password=root") or die("Can't connect to database".pg_last_error());
$result = pg_query('SELECT * FROM playground');
$rows = array();
while($r = pg_fetch_assoc($result)) {
$rows[] = $r;
}
print json_encode(array_values(pg_fetch_all($result)));
?>
这个returns下面的对象
[{"name":"slide","color":"blue","location":"south"},{"name":"slide","color":"green","location":"north"},{"name":"dont","color":"red","location":"west"}]
但是,我正在尝试将它们存储在更像....
[
{"name":"slide","values": [ "color" : ["blue","green"], "location": ["south", "north"] ]},
{"name":"dont","values" : ["color" : ["red"],"location" : ["west"] }
]
基本上,我想按父字段值作为键对公共组进行分组,并将它们的值作为 json 对象的值。
但是我做不到。如果有人可以提供帮助,将不胜感激。 谢谢。
您可以循环遍历$result
并自己创建所需的数组结构:
while($r = pg_fetch_assoc($result)) {
if (!array_key_exists($r['name'], $rows)) {
$rows[$r['name']] = array('values' => array( 'color' => array(), 'location' => array() ));
}
if (!in_array($r['color'], $rows[$r['name']]['values']['color'])) {
$rows[$r['name']]['values']['color'][] = $r['color'];
}
if (!in_array($r['location'], $rows[$r['name']]['values']['location'])) {
$rows[$r['name']]['values']['location'][] = $r['location'];
}
}
echo json_encode($rows);
这将在表格
上生成 JSON{
"slide": {
"values": {
"color": [
"blue",
"green"
],
"location": [
"south",
"north"
]
}
},
"dont": {
"values": {
"color": [
"red"
],
"location": [
"west"
]
}
}
}