How to pass an array into a function, and return the results with an array

后端 未结 9 1396
你的背包
你的背包 2021-02-05 07:25

So I\'m trying to learn how to pass arrays through a function, so that I can get around PHP\'s inability to return multiple values. Haven\'t been able to get anything to work so

相关标签:
9条回答
  • 2021-02-05 08:12

    You seem to be looking for pass-by-reference, to do that make your function look this way (note the ampersand):

    function foo(&$array)
    {
        $array[3]=$array[0]+$array[1]+$array[2];
    }
    

    Alternately, you can assign the return value of the function to a variable:

    function foo($array)
    {
        $array[3]=$array[0]+$array[1]+$array[2];
        return $array;
    }
    
    $waffles = foo($waffles)
    
    0 讨论(0)
  • 2021-02-05 08:14

    Here is how I do it. This way I can actually get a function to simulate returning multiple values;

    function foo($array) 
    { 
        foreach($array as $_key => $_value) 
        {
           $str .= "{$_key}=".$_value.'&';
        }
        return $str = substr($str, 0, -1);
    } 
    
    /* Set the variables to pass to function, in an Array */
    
        $waffles['variable1'] = "value1"; 
        $waffles['variable2'] = "value2"; 
        $waffles['variable3'] = "value3"; 
    
    /* Call Function */
    
        parse_str( foo( $waffles ));
    
    /* Function returns multiple variable/value pairs */
    
        echo $variable1 ."<br>";
        echo $variable2 ."<br>";
        echo $variable3 ."<br>";
    

    Especially usefull if you want, for example all fields in a database to be returned as variables, named the same as the database table fields. See 'db_fields( )' function below.

    For example, if you have a query

    select login, password, email from members_table where id = $id

    Function returns multiple variables:

    $login, $password and $email

    Here is the function:

    function db_fields($field, $filter, $filter_by,  $table = 'members_table') {
    
     /*
        This function will return as variable names, all fields that you request, 
        and the field values assigned to the variables as variable values.  
    
        $filter_by = TABLE FIELD TO FILTER RESULTS BY
        $filter =  VALUE TO FILTER BY
        $table = TABLE TO RUN QUERY AGAINST
    
        Returns single string value or ARRAY, based on whether user requests single
        field or multiple fields.
    
        We return all fields as variable names. If multiple rows
        are returned, check is_array($return_field); If > 0, it contains multiple rows.
        In that case, simply run parse_str($return_value) for each Array Item. 
    */
        $field = ($field == "*") ? "*,*" : $field;
        $fields = explode(",",$field);
    
        $assoc_array = ( count($fields) > 0 ) ? 1 : 0;
    
        if (!$assoc_array) {
            $result = mysql_fetch_assoc(mysql_query("select $field from $table where $filter_by = '$filter'"));
            return ${$field} = $result[$field];
        }
        else
        {
            $query = mysql_query("select $field from $table where $filter_by = '$filter'");
            while ($row = mysql_fetch_assoc($query)) {
                foreach($row as $_key => $_value) {
                    $str .= "{$_key}=".$_value.'&';
                }
                return $str = substr($str, 0, -1);
            }
        }
    }
    

    Below is a sample call to function. So, If we need to get User Data for say $user_id = 12345, from the members table with fields ID, LOGIN, PASSWORD, EMAIL:

    $filter = $user_id;
    $filter_by = "ID";
    $table_name = "members_table"
    
    parse_str(db_fields('LOGIN, PASSWORD, EMAIL', $filter, $filter_by, $table_name));
    
    /* This will return the following variables: */
    
    echo $LOGIN ."<br>";
    echo $PASSWORD ."<br>";
    echo $EMAIL ."<br>";
    

    We could also call like this:

    parse_str(db_fields('*', $filter, $filter_by, $table_name));
    

    The above call would return all fields as variable names.

    0 讨论(0)
  • 2021-02-05 08:15

    i know a Class is a bit the overkill

    class Foo
    {
    
     private $sum = NULL;
    
     public function __construct($array)
     {
       $this->sum[] = $array;
       return $this;
     }
    
     public function getSum()
     {
       $sum = $this->sum;
       for($i=0;$i<count($sum);$i++)
       {
          // get the last array index
          $res[$i] = $sum[$i] + $sum[count($sum)-$i];
       }
       return $res;
     }
    
    
    }
    
    
    $fo = new Foo($myarray)->getSum();
    
    0 讨论(0)
提交回复
热议问题