使用JSON搜索JSON

肖恩·T

我是JSON和PHP新手,需要一些帮助!我有一些json,想user用_GET搜索,并返回有关该用户的所有信息。

JSON:

{
Shawn Taylor: {
  user: "Shawn Taylor",
  email: "[email protected]",
  phone: "604-123-4567"
  },
John Smith: {
  user: "John Smith",
  email: "[email protected]",
  phone: "604-123-4569"
  }

形式:

<form method="get">
  <input name="find" />
  <button type="submit">Find</button>
</form>

PHP:

if (!empty($_GET['find'])){
  $find = ($_GET['find']);
  $data_url = 'data.json';
  $data_json = file_get_contents($data_url);
  $data_array = json_decode($data_json, true);
  echo $data_array['user'];
  echo $data_array['email'];
  echo $data_array['phone'];

我认为这应该可行,但是没有运气。有人可以帮忙吗?

阿尔法卢吉

您只是忘了用$find钥匙找到它

尝试一下(看最后4行):

if (!empty($_GET['find'])){
  $find = ($_GET['find']);
  $data_url = 'data.json';
  $data_json = file_get_contents($data_url);
  $data_array = json_decode($data_json, true);

  $user = $data_array[$find];
  echo $user['user'];
  echo $user['email'];
  echo $user['phone'];
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章