我正在现场表演 我用它来获取json文件
<?php
if(mysqli_num_rows($result) > 0)
{
while($row1 = mysqli_fetch_array($result))
{
$output["name"][]=$row1["name"];
$output["email"][]=$row1["email"];
}
}
$fp = fopen('results.json', 'w');
fwrite($fp,json_encode($output));
fclose($fp);
?>
我在json文件中得到了类似的内容
{
"name":["Marinasy","test","test","Nath"],
"email":["behambymarinasy@gmail.com","test@test","test@trs","nath@trs"]
}
但是我需要类似下面的代码来进行搜索。有什么办法可以得到像这样的JSON而不是上面的JSON? 或如何在上面的代码中搜索?
[
{
"name":"Marinasy",
"email": "behambymarinasy@gmail.com"
},
{
"name":"Test",
"email": "test@test"
},
{
"name":"Nath",
"email": "nath@trs"
}
]
答案 0 :(得分:2)
您可以这样做...
<?php
$output= array();
if(mysqli_num_rows($result) > 0)
{
while($row1 = mysqli_fetch_array($result))
{
array_push($output, array('name'=> $row1["name"], 'email'=> $row1["email"]));
}
}
$fp = fopen('results.json', 'w');
fwrite($fp,json_encode($output));
fclose($fp);
?>
答案 1 :(得分:0)
更改此
$output["name"][]=$row1["name"];
$output["email"][]=$row1["email"];
到
$output[] = [
"name"=>$row1["name"],
"email"=>$row1["email"]
];
完整代码在这里
<?php
if (mysqli_num_rows($result) > 0) {
$output = [];
while ($row1 = mysqli_fetch_array($result)) {
$output[] = [
"name" => $row1["name"],
"email" => $row1["email"]
];
}
}
$fp = fopen('results.json', 'w');
fwrite($fp, json_encode($output));
fclose($fp);
?>