2015-11-22 08:32
浏览 844


I am getting a Json respond with :

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

I manage to get its values(strings) with :


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) 


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

    • Build proper initial json. Instead of

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

      should be

      {"phoneNumber": ["xx665542","xxx9446"]}
    点赞 评论
  • duanan2732 2015-11-22 08:36
    foreach ($foundPhoneNum as $value) 

    There was an extra & before $value. Try this.

    点赞 评论
  • douxia6554 2015-11-22 08:41

    There's a couple of ways to debug situations like this as mentioned in the comments; print_r() and var_dump().

    var_dump(), although harder to read the first few times, is my favourite because it tells you the data types of each value in the array. This will confirm whether or not the expected string is indeed an array.

    An example from the var_dump() documentation:

    $a = array(1, 2, array("a", "b", "c"));

    And the output is;

    array(3) {
      array(3) {
        string(1) "a"
        string(1) "b"
        string(1) "c"

    As you can see it shows array, int and string as the data types.

    You might also like to install the Xdebug extension for PHP which dumps more useful error messages and tracebacks. Again harder to read the first few times, but well worth it!

    点赞 评论

相关推荐 更多相似问题