doujia7162 2015-03-06 08:40
浏览 63
已采纳

使用PHP搜索JSON

I'm a JSON and PHP noob, and need some help! I've got some json, and want to search by user with _GET, and return all the info about that user.

JSON:

{
Shawn Taylor: {
  user: "Shawn Taylor",
  email: "email@example.com",
  phone: "604-123-4567"
  },
John Smith: {
  user: "John Smith",
  email: "email2@example.com",
  phone: "604-123-4569"
  }

FORM:

<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'];

I thought this should work, but no such luck. Can anyone help please?

  • 写回答

2条回答 默认 最新

  • drtwqc3744 2015-03-06 08:49
    关注

    You just forgot to locate it with the $find key.

    Try that (look at the 4 last lines) :

    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'];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私