Home > database >  How to get return value in array from other function in php?
How to get return value in array from other function in php?

Time:11-23

I want to pass values in array data type to other function(method), but it only send first part of data. in selectDataType() function when print_r command it expect to show other value but show only first record. it work find in selectColumns() function if I print_r $column variable. how can get all records in array.

//get data from URL - api mode
public function getDataFromUrl() {
    $url =  "http://www.thecocktaildb.com/api/json/v1/1/search.php?s=margarita";
    $json = json_decode(file_get_contents($url), true);
    return $json;
    
}

//select columns
public function selectColumns(){
    $json = $this->getdataFromUrl();
    foreach($json['drinks'] as $columns) {
        
          $column['col1'] = $columns['idDrink'] ;
          $column['col2'] = $columns['strDrink'] ;
          return $column;
         
    }              
}

//select data types
 public function selectDataType(){
     $json = $this->selectColumns();         
     print_r($json);

 }

show only : Array ( [col1] => 11007 [col2] => Margarita )

correct result must be: Array ( [col1] => 11007 [col2] => Margarita ) Array ( [col1] => 11118 [col2] => Blue Margarita ) Array ( [col1] => 17216 [col2] => Tommy's Margarita ) Array ( [col1] => 16158 [col2] => Whitecap Margarita ) Array ( [col1] => 12322 [col2] => Strawberry Margarita ) Array ( [col1] => 178332 [col2] => Smashed Watermelon Margarita )

CodePudding user response:

$column['col1'] =... keeps overwriting the value of $column['col1'] every time you loop. So if the code looped multiple times, you'd only ever see the last result.

But you're only seeing the first result, which is because the position of your return makes it return when you've only added the first record anyway...if you return inside a loop, it exits the function, which means it obviously won't be able to loop again.

You need to

  1. create an array to hold all the drinks before you start looping

  2. create an individual variable to hold the data about each drink, within the loop, and then add that item to the list

  3. when you've finished looping, return the whole list.

For example:

public function selectColumns(){
    $drinks = $this->getdataFromUrl();
    $drinkList = [];
    
    foreach($json['drinks'] as $columns) {
          //create an item representing a drink
          $drink = array
          (
            'col1' => $columns['idDrink'],
            'col2' => $columns['strDrink']
          );
          $drinkList[] = $drink; //add the drink to the list
    }
    return $drinkList; ///after we've finished looping, return the whole list
}

CodePudding user response:

What you need to return is multi-dimensional array:

public function selectColumns()
{
    $json = $this->getdataFromUrl();

    $column = [];

    foreach ($json['drinks'] as $columns) {
        $column[] = [
            'col1' => $columns['idDrink'],
            'col2' => $columns['strDrink'],
        ];
    }

    return $column;
}
  • Related