I'm do a live我用它来获取 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"
}
]
你可以这样做...
<?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);
?>
更改此内容
$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);
?>