在 JSON 文件中搜索

Search in a JSON file

我在直播 我用它来获取 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);
?>