How to extract $data array from this function?

 function kv_euro() { $path = CACHE_DIR . 'weather_' . weather_city('city') . '.json'; $json = get_cache_value($path); if (false !== $json) { echo ""; $data = json_decode($json); //echo $json; } else { $BASE_URL = "http://api.openweathermap.org/data/2.5/forecast/daily"; //city //$yql_query = 'select * from weather.forecast where woeid="' . weather_city('id') . '"'; $yql_query_url = $BASE_URL . "?id=" . weather_city('id') . "&APPID=9e2227183d6b7351f6942dbaac4d24af"; // Make call with cURL $session = curl_init($yql_query_url); curl_setopt($session, CURLOPT_RETURNTRANSFER, true); $json = curl_exec($session); $data = json_decode($json); set_cache_value($path, $json); } } 

I put return $data , but I can't see it all the same.

 <?php print_r($data) ?> 
  • and so? <?php print_r(kv_euro()) ?> and return $data - Grundy

4 answers 4

Here everything rests on what you mean by "pull out the array" ... In any case, to get it, you need to return it:

 function kv_euro() { $path = CACHE_DIR . 'weather_' . weather_city('city') . '.json'; $data = []; $json = get_cache_value($path); if (false !== $json) { echo ""; $data = json_decode($json, true); //echo $json; } else { $BASE_URL = "http://api.openweathermap.org/data/2.5/forecast/daily"; //city //$yql_query = 'select * from weather.forecast where woeid="' . weather_city('id') . '"'; $yql_query_url = $BASE_URL . "?id=" . weather_city('id') . "&APPID=9e2227183d6b7351f6942dbaac4d24af"; // Make call with cURL $session = curl_init($yql_query_url); curl_setopt($session, CURLOPT_RETURNTRANSFER, true); $json = curl_exec($session); $data = json_decode($json, true); set_cache_value($path, $json); } return $data; } 

Write here:

 $data = kv_euro(); 

And if you display on the screen, then: print '<pre>'.print_r($data, true).'</pre>';

    To return the data to the $data variable, use the return function.

     function kv_euro() { // тут ваш код // $data = json_decode($json); return $data; } $data = kv_euro(); // Вот и вытащили $data // В вашем случае $data равняется объекту или NULL 

    json_decode defaults to return объект or NULL if an error json_decode . If you want to get an array, pass the second parameter true

     $data = json_decode($json, true); 
       function kv_euro() { $data = []; //some code if() { //какой-то код $data = json_decode($someVar); } else {//some other code $data = json_decode($someVar); } return $data; } 

      About this tells you @PinkTux

        Read about the scope of variables.

         /* здесь пока никакой переменной $data нет */ if (false !== $json) { /* создалась переменная $data, локальная для этого блока */ $data = json_decode($json); /* и тут она уже пропала */ } else { /* создалась ДРУГАЯ переменная $data, локальная для этого блока */ $data = json_decode($json); /* и тут она тоже пропала */ } /* и здесь никакой переменной $data нет */ 

        Either return $data inside each block separately (not the best option), or declare this variable BEFORE using inside if/else blocks.

        • why not the best option? - Grundy
        • Because in this case there is no reasonable justification for several exit points from the function. - PinkTux
        • well, why? did a quick check and returned the result, and if the check did not go through doing something further, the extra else is removed, the code nesting becomes less for example, the problem with the scopes goes away altogether - Grundy
        • Well, to rewrite the code is another question, then it may be simpler and it will be clearer :) - PinkTux
        • The nesting of the code does not change, its volume will increase, dear @Grundy - Hlogeon