user3724476
user3724476

Reputation: 5140

Decoding JSON SubArray PHP

I have looked around on Google for quite some time now, and I'm unable to find a solution that works for me.

I have this JSON Array:

[
 {
  "id": 1000,
  "userIdent": "ABC1000",
  "username": "john.doe",
  "contacts": [
   {
    "id": 2000,
    "clientId": 1000,
    "email": "[email protected]",
    "phone": "",
    "name": "",
    "isBilling": false,
    "isContact": false,
    "types": [
      {
        "id": 3000,
        "name": "Home contact"
      }
    ]
  }
]
}
]

and I have this PHP code:

$json = json_decode($response, true);

foreach($json as $item) { 
    echo $item['id'] . "<br>";
    echo $item['userIdent'] . "<br>";
    echo $item['contacts']['phone'] . "<br><br>";
        foreach($json->contacts as $contacts) {
            echo $contacts->phone;
            echo $contacts['contacts']['phone'];
        }

}

I have tried:

$item['contacts']['phone'];
$contacts->phone;
$contacts['contacts']['phone'];

I can't seem to be able to full any of the data from the sub-array "contacts". Any help would be greatly appreciated!

Upvotes: 1

Views: 1999

Answers (3)

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72269

Note:- When you use true while using json_decode() it converts all json data to array(inner or outer). So you need to treat contacts as an array not an object.

So You need to iterate over $item['contacts'] array in the second foreach()

Do like below:-

$json = json_decode($response, true);

foreach($json as $item) { 
  echo $item['id'] . "<br>";
  echo $item['userIdent'] . "<br>";
  foreach($item['contacts'] as $contacts) {//treat contacts as an array not as an object
    echo $contacts['phone'];
  }
}

Output:- https://eval.in/952121 (i have taken phone number for testing purpose)

Upvotes: 3

Pevara
Pevara

Reputation: 14310

Your code should look more like this

   foreach($json as $item) {                      // 1
        echo $item['id'] . "<br>";
        echo $item['userIdent'] . "<br>";
        foreach($item['contacts'] as $contact) {    // 2
           echo $contact['phone'];
        }
    }

So in the first foreach you start iterating over you entire array of items (1). Inside an item is an array contacts, so you start iterating over that with a new foreach (2). Each contact can be accessed directly inside the inner foreach.

Also, on decoding you said you wanted an array as output, so you should expect that and always access it like an array (square braces). If you would have gone for an object, the $contacts->phone syntax would work, but you shouldn't mix them like you are doing.

I hope this makes sense. Feel free to ask if not.

Upvotes: 0

Pierre
Pierre

Reputation: 649

You have made a mistake, on your json, the contacts property is an array, so you can't access it (either in javascript or php) as an object with $contacts->contacts['phone'].

You should do something like that : $contacts->contacts[0]['phone'] or iterate other each contacts if there may be many.

Upvotes: 0

Related Questions