I have a JSON array
{
"people":[
{
"id": "8080",
"content": "foo"
},
{
"id": "8097",
"content": "bar"
}
]
}
How would I search for 8097 and get the content?
Use the json_decode
function to convert the JSON string to an array of object, then iterate through the array until the desired object is found:
$str = '{
"people":[
{
"id": "8080",
"content": "foo"
},
{
"id": "8097",
"content": "bar"
}
]
}';
$json = json_decode($str);
foreach ($json->people as $item) {
if ($item->id == "8097") {
echo $item->content;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With