My example json data is below, I have tried to parse it however, I couldn't get the data with $response[0] or $response[1] etc... How can I parse it?
Thanks!
[
{
"Tags": [
"diyarbakır",
"bağlar",
"patlama"
]
},
{
"Tags": [
"gazetehaberleri",
"galatasaray lisesi",
"kadri gürsel"
]
}
]
UPDATE
$response = json_decode($response);
foreach($response as $key => $value){
echo $value;
}
Warning: Invalid argument supplied for foreach()
$response = json_decode($response, true);
by default second argument is false. by using true it forces it to associative array.
As hinted upon by Varuog, the reason you cannot access the elements properly is because you seem to be confusing the result of json_decode()
. By default, it converts the JSON objects into PHP objects.
You should still be able to get the data using $response[0]
and $response[1]
, however the way you access the data from there is different.
For your current implementation, to access the "Tags" element, you must do:
$response = json_decode($response);
foreach($response as $key => $value){
print_r($value->{'Tags'});
}
Which gives the output:
Array
(
[0] => diyarbakır
[1] => bağlar
[2] => patlama
)
If you set the second argument of json_decode()
to true
, it will convert the objects to arrays and you can access it via $value['Tags']
:
$response = json_decode($response, true);
foreach($response as $key => $value){
print_r($value['Tags']);
}