dongzhan2029 2016-10-06 14:58
浏览 972
已采纳

从特定键的每个json对象获取值

In my one project i required to extract each phone from input json. Json input something like:

[
      {
          "name": "Niyo",
        "email": "niyo@ymail.com",
        "phone": "8989457845"
      },
      {
          "name": "Picks",
        "email": "picks.p@gmail.com",
        "phone": "7878121245"
      },
      {
          "name": "Chintz",
        "email": "Chintz@gmail.com",
        "phone": "8745421254"
      },
      {
          "name": "Kabiru Wabyu",
        "email": "kabiru.v@gmail.com",
        "phone": ""
      },
      {
          "name": "Rons",
        "email": "",
        "phone": "9898989898"
      }
]

I know one solutions to extract each phone from input json contact

foreach($contacts as $phone){ $phones[]=$phone->phone; }

Is there any alternate way in php/laravel to get all values from input json for specific key ?

  • 写回答

3条回答 默认 最新

  • doushu0591 2016-10-06 15:07
    关注

    This should return what you're seeking, provided that you have php 5.5 or more recent. Not sure if it's necessary to set the second parameter of json_decode to TRUE, which returns an array of arrays, rather than array of objects.

    $x = json_decode(yourjson, TRUE);
    $phones = array_column($x, 'phone');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试