Let's assume I have the following JSON object
$object =
{
"recipe": {
"apples": 5
"flour": "2 lbs"
"milk": "2 cartons"
}
}
Now consider this function
private function get_quantity($ingredient) {
$json = json_decode($object);
return $json->recipe->{$ingredient};
}
If I pass milk to the function and want to get 2 cartons as the output. Is this possible in PHP?
return (isset($json->recipe->$ingredient)) ? $json->recipe->$ingredient : null;