我要转换的JSON文件键:值对,用于如何使用PHP或JS将JSON转换为值 - 密钥对?
[
{"value":"apple","label":"apple"},
{"value":"Google","label":"Google"}
]
像下面键:值对格式,这样
{
"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);
?>
感谢罗马人,你解决了我的问题,最后一件事我该如何追加结果,在一个文本文件文件后r var data = {“apple”:“apple”,“Google”:“Google”,。 。 。 。 。 。 。 。 。 。 。 。 }“ –
使用'file_put_contents('textvalues.txt',$ textval,FILE_APPEND)'将数据附加到文件而不是覆盖它 – RomanPerekhrest
谢谢,但是当我再次运行文件时,它会附加上以前的数据。因此,如何刷新文件得到刷新前的变化 –