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

报告相同问题?