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条)

报告相同问题?