dongpian6319 2015-11-22 08:32
浏览 890
已采纳

从Json中获取数组?

I am getting a Json respond with :

$response = curl_exec($rest);  
 $json = json_decode($response, true);

I manage to get its values(strings) with :

$foundUserId=$json['results'][0]['userId'];
$foundName=$json['results'][0]['name'];
$foundPhoneNum=$json['results'][0]['phoneNumber'];

But the last value- phoneNumber, is array of strings .

If i try then to loop over it i get nothing(although the array is there in the Json)

  foreach ($foundPhoneNum as &$value) 
    {
      print_r($value);

    }

What am i doing wrong ?

EDIT : The json:

Array ( [results] => Array ( [0] => Array ( [action] => message [createdAt] => 2015-11-21T09:36:33.620Z [deviceId] => E18DDFEC-C3C9 [name] => me [objectId] => klMchCkIDi [phoneNumber] => ["xx665542","xxx9446"] [state] => 1 [updatedAt] => 2015-11-22T08:24:46.948Z [userId] => 433011AC-228A-4931-8700-4D050FA18FC1 ) ) )  
  • 写回答

3条回答 默认 最新

  • dongzhuo1498 2015-11-22 08:49
    关注

    You might have json as a string inside json. That's why after json_decode() you still have json inside phoneNumber. You have 2 options:

    • Decode phoneNumber like

      $foundPhoneNum=json_decode($json['results'][0]['phoneNumber']);
      
    • Build proper initial json. Instead of

      {"phoneNumber": "[\"xx665542\",\"xxx9446\"]"}
      

      should be

      {"phoneNumber": ["xx665542","xxx9446"]}
      
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题