How do I get the string (3h) from this JSON object only using "rain" with php (json_decode)?
{
"rain": {
"3h": 3.5
}
}
There are a few options for getting at the properties of that rain object, which is, in essence, what you want to do:
$json = '{
"rain": {
"3h": 3.5
}
}';
$obj = json_decode($json);
$rain = $obj->rain;
$rain_properties = get_object_vars($rain);
// you now have an associative array that lists all keys and values for properties of the object
// you can look at the keys using
$rain_keys = array_keys($rain_properties);
echo $rain_keys[0]; // would give '3h' in this example
// or, you can iterate through the properties
foreach($rain_properties as $key => $value) {
echo $key; // would give '3h' on first iteration in this example
echo $value; // would give 3.5 in this example
}