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 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥30 用arduino开发esp32控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿