Hellos Posted January 1, 2023 Share Posted January 1, 2023 $data = file_get_contents($url); // put the contents of the file into a variable $characters = json_decode($data); // decode the JSON feed foreach ($characters as $character) { //location $name = $character->name; $region = $character->region; $country = $character->country; $lat = $character->lat; $lon = $character->lon; $tz_id = $character->tz_id; $localtime_epoch = $character->localtime_epoch; $localtime = $character->localtime; } echo$name; ?> This is my code and name will not echo the content. $data = file_get_contents($url); // put the contents of the file into a variable $characters = json_decode($data); // decode the JSON feed foreach ($characters as $character) { //location $name = $character->name; $region = $character->region; $country = $character->country; $lat = $character->lat; $lon = $character->lon; $tz_id = $character->tz_id; $localtime_epoch = $character->localtime_epoch; $localtime = $character->localtime; echo$name; } ?> The variable name will output correctly here. Does anyone know what the problem is. Is it to do with variable scopes like global, static or something? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/315747-php-variable-not-accessible/ Share on other sites More sharing options...
Barand Posted January 1, 2023 Share Posted January 1, 2023 In your first code block you read each object putting the name in to the same variabe, thus each one overwrites the previous. Probably, if the last object is empty, or the name is empty, nothing will print. In the second block you echo each name as it is read. Without seeing the content of $data or $characters then cannot be sure. Quote Link to comment https://forums.phpfreaks.com/topic/315747-php-variable-not-accessible/#findComment-1604082 Share on other sites More sharing options...
Hellos Posted January 2, 2023 Author Share Posted January 2, 2023 <?php //echo $character->is_day . '<br>'; $url = '{"location":{"name":"London","region":"City of London, Greater London","country":"United Kingdom","lat":51.52,"lon":-0.11,"tz_id":"Europe/London","localtime_epoch":1672618641,"localtime":"2023-01-02 0:17"},"current":{"last_updated_epoch":1672618500,"last_updated":"2023-01-02 00:15","temp_c":8.0,"temp_f":46.4,"is_day":0,"condition":{"text":"Light rain","icon":"//cdn.weatherapi.com/weather/64x64/night/296.png","code":1183},"wind_mph":4.3,"wind_kph":6.8,"wind_degree":220,"wind_dir":"SW","pressure_mb":1009.0,"pressure_in":29.8,"precip_mm":0.0,"precip_in":0.0,"humidity":93,"cloud":25,"feelslike_c":7.0,"feelslike_f":44.7,"vis_km":10.0,"vis_miles":6.0,"uv":1.0,"gust_mph":5.8,"gust_kph":9.4}}'; $data = file_get_contents($url); // put the contents of the file into a variable $characters = json_decode($data); // decode the JSON feed foreach ($characters as $character) { //location $name = $character->name; $region = $character->region; $country = $character->country; $lat = $character->lat; $lon = $character->lon; $tz_id = $character->tz_id; $localtime_epoch = $character->localtime_epoch; $localtime = $character->localtime; echo$name; } ?> Here is the full code. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/315747-php-variable-not-accessible/#findComment-1604111 Share on other sites More sharing options...
Hellos Posted January 2, 2023 Author Share Posted January 2, 2023 It works now, you have to use concatenation. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/315747-php-variable-not-accessible/#findComment-1604112 Share on other sites More sharing options...
Hellos Posted January 2, 2023 Author Share Posted January 2, 2023 @Barandone more thing, How do I get the data of the values in condition? Because it is an array in array Thanks Quote Link to comment https://forums.phpfreaks.com/topic/315747-php-variable-not-accessible/#findComment-1604115 Share on other sites More sharing options...
Solution Barand Posted January 2, 2023 Solution Share Posted January 2, 2023 $data = '{"location":{"name":"London","region":"City of London, Greater London","country":"United Kingdom","lat":51.52,"lon":-0.11,"tz_id":"Europe/London","localtime_epoch":1672618641,"localtime":"2023-01-02 0:17"},"current":{"last_updated_epoch":1672618500,"last_updated":"2023-01-02 00:15","temp_c":8.0,"temp_f":46.4,"is_day":0,"condition":{"text":"Light rain","icon":"//cdn.weatherapi.com/weather/64x64/night/296.png","code":1183},"wind_mph":4.3,"wind_kph":6.8,"wind_degree":220,"wind_dir":"SW","pressure_mb":1009.0,"pressure_in":29.8,"precip_mm":0.0,"precip_in":0.0,"humidity":93,"cloud":25,"feelslike_c":7.0,"feelslike_f":44.7,"vis_km":10.0,"vis_miles":6.0,"uv":1.0,"gust_mph":5.8,"gust_kph":9.4}}'; $characters = json_decode($data, 1); // decode the JSON feed as array echo $characters['location']['name'] . '<br>'; echo $characters['current']['condition']['text'] . '<br>'; echo $characters['current']['condition']['code'] . '<br>'; If you want to stay with objects... $data = '{"location":{"name":"London","region":"City of London, Greater London","country":"United Kingdom","lat":51.52,"lon":-0.11,"tz_id":"Europe/London","localtime_epoch":1672618641,"localtime":"2023-01-02 0:17"},"current":{"last_updated_epoch":1672618500,"last_updated":"2023-01-02 00:15","temp_c":8.0,"temp_f":46.4,"is_day":0,"condition":{"text":"Light rain","icon":"//cdn.weatherapi.com/weather/64x64/night/296.png","code":1183},"wind_mph":4.3,"wind_kph":6.8,"wind_degree":220,"wind_dir":"SW","pressure_mb":1009.0,"pressure_in":29.8,"precip_mm":0.0,"precip_in":0.0,"humidity":93,"cloud":25,"feelslike_c":7.0,"feelslike_f":44.7,"vis_km":10.0,"vis_miles":6.0,"uv":1.0,"gust_mph":5.8,"gust_kph":9.4}}'; $obj = json_decode($data); // decode the JSON feed as object echo $obj->location->name . '<br>'; echo $obj->current->condition->text . '<br>'; echo "<img src='{$obj->current->condition->icon}'>" . '<br>'; echo $obj->current->condition->code . '<br>'; 1 Quote Link to comment https://forums.phpfreaks.com/topic/315747-php-variable-not-accessible/#findComment-1604119 Share on other sites More sharing options...
Hellos Posted January 3, 2023 Author Share Posted January 3, 2023 @BarandThank you very much! Quote Link to comment https://forums.phpfreaks.com/topic/315747-php-variable-not-accessible/#findComment-1604138 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.