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

后端 未结 9 1421
你的背包
你的背包 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: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 ."
    "; echo $variable2 ."
    "; echo $variable3 ."
    ";

    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 ."
    "; echo $PASSWORD ."
    "; echo $EMAIL ."
    ";

    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.

提交回复
热议问题