Question: Want to parse JSON object array using PHP, but I don't know how to do it as I am new to PHP.

Here is my JSON (Structure can't be altered since the JSON is constructed by other system):

```
[
{
"animalName":"Fox",
"animalId":"1"
},
{
"animalName":"Elephant",
"animalId":"2"
},
{
"animalName":"Monkey",
"animalId":"3"
},
{
"animalName":"Donkey",
"animalId":"4"
}
]

```

I used json\_decode function to decode it but I dont know how to read values inside the json.

For ex: How would I get the value Monkey by parsing the JSON.

Thanks in advance for your help!

Answer: 
This will do it

```
$data = json_decode($json);

$data[0]->animalName; // "Fox"

```

Per a useful comment

```
<pre>
<?php print_r($data);

```
If you want to get all in a loop you can do as

```
$str='[
{
"animalName":"Fox",
"animalId":"1"
},
{
"animalName":"Elephant",
"animalId":"2"
},
{
"animalName":"Monkey",
"animalId":"3"
},
{
"animalName":"Donkey",
"animalId":"4"
}
]';

$data = json_decode($str,true);

foreach($data as $k=>$v){
 echo "Animal Name :".$v["animalName"]."<br />";
  echo "Animal ID :".$v["animalId"]."<br />";
}

```