php - Remove square brackets from child rows in json
Get the solution ↓↓↓I am trying to display the following data on a website :-
"daily":[{"dt":1593864000,"sunrise":1593834201,"sunset":1593894929,"temp":{"day":18.47,"min":17.83,"max":18.71,"night":17.83,"eve":18.71,"morn":18.47},"feels_like":{"day":16,"night":13.09,"eve":16.54,"morn":16},"pressure":1006,"humidity":77,"dew_point":14.37,"wind_speed":5.51,"wind_deg":244,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"clouds":90,"uvi":7.08}
I extract any of the entries except the ones in the weather section as the code I'm using thinks that the weather data is a separate array.
The section of code relevant to displaying the data is :-
<span class="min-temperature"> Minimum Temperature <?php echo $data->daily[0]->clouds; ?>°C</span><br>
<span class="min-temperature"> Pressure <?php echo $data->daily[0]->weather->id; ?></span>
The first line displays data fine but anything within the weather section fails to display anything.
I've seen solutions to remove all the square brackets but its only the brackets surrounding the weather section that is needed.
Thanks in advance
Answer
Solution:
the code below json_decodes and echoes cloud and the weather array. 'hope it helps. please comment. thank you.
<?php
$data=json_decode( '{"daily":{"dt":1593864000,"sunrise":1593834201,"sunset":1593894929,"temp":{"day":18.47,"min":17.83,"max":18.71,"night":17.83,"eve":18.71,"morn":18.47},"feels_like":{"day":16,"night":13.09,"eve":16.54,"morn":16},"pressure":1006,"humidity":77,"dew_point":14.37,"wind_speed":5.51,"wind_deg":244,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"clouds":90,"uvi":7.08}}'); # define $data as a stdClass Object
echo $data->daily->clouds;
echo "\n";
# below, weather array is converted into a string
$wa=(array)$data->daily->weather[0];
foreach($wa as $key=> $val){
echo $key."=".$val."; ";
}
?>
Output:
90
id=500; main=Rain; description=light rain; icon=10d;
Answer
Solution:
You must usejson_decode
in this case to convert a json string to associative array.
$data = '{"daily":{"dt":1593864000,"sunrise":1593834201,"sunset":1593894929,"temp":{"day":18.47,"min":17.83,"max":18.71,"night":17.83,"eve":18.71,"morn":18.47},"feels_like":{"day":16,"night":13.09,"eve":16.54,"morn":16},"pressure":1006,"humidity":77,"dew_point":14.37,"wind_speed":5.51,"wind_deg":244,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"clouds":90,"uvi":7.08}}';
$decode = json_decode($data,true);
echo '<pre>';
//print_r($decode);
echo $decode['daily']['clouds'].'<br>';
echo $decode['daily']['uvi'].'<br>';
echo $decode['daily']['weather'][0]['id'].'<br>';
echo $decode['daily']['weather'][0]['main'].'<br>'; //These three are from weather array.
echo $decode['daily']['weather'][0]['description'].'<br>';
echo '<pre>';
Output
90
7.08
500
Rain
light rain
If you want to know how the array indexes works you can make use ofprint_r
from the code just remove it from comments.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: script cache:clear returned with error code 255
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.