【发布时间】:2017-02-28 10:20:47
【问题描述】:
我想将 JSON 文件转换为 key:value 对,意思是
[
{"value":"apple","label":"apple"},
{"value":"Google","label":"Google"}
]
如下 key:value 对格式,如下所示
{
"apple": "apple",
"google": "google",
.......
.....
......
......
}
所以任何人都可以告诉我我该怎么做,下面是 Php 文件代码,我从 psql 数据库中获取数据并将其存储在一个文件中。
dbconn.php
<?php
$db = pg_connect("host=localhost port=5432 dbname=postgres user=postgres password=root123");
pg_select($db, 'post_log', $_POST); //selecting database
$query=pg_query("SELECT name FROM account");
$json=array();
while($student=pg_fetch_array($query)){
$json[]=array(
'value'=> $student["name"],
'label'=>$student["name"]);
}
$textval = json_encode($json); //encoding
file_put_contents('textvalues.txt', $textval);
?>
【问题讨论】: