Hướng dẫn này giải thích cách truyền một mảng làm đối số trong một hàm với PHP. Quá trình chuyển một mảng vào một hàm rất giống với việc chuyển một biến vào một hàm. Xem video bên dưới và sau đó cuộn xuống để xem mã mẫu. //codemahal.com/wp-content/uploads/2016/03/passing-an-array-into-a-feunction.mp4 Mã PHP mẫu:
Đây là cách tôi làm điều đó. Bằng cách này, tôi thực sự có thể nhận được một hàm để mô phỏng trả về nhiều giá trị;
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 ."
";
Đặc biệt hữu ích nếu bạn muốn, ví dụ tất cả các trường trong cơ sở dữ liệu được trả về dưới dạng các biến, được đặt tên giống như các trường bảng cơ sở dữ liệu. Xem hàm 'db_fields []' bên dưới.all fields in a database to be returned as variables, named the same as the database table fields. See 'db_fields[ ]' function below.
Ví dụ: nếu bạn có một truy vấn
select login, password, email from members_table
where id = $id
Hàm trả về nhiều biến:
$login, $password and $email
Đây là chức năng:
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];
}
}
}
Dưới đây là một cuộc gọi mẫu để chức năng. Vì vậy, nếu chúng ta cần lấy dữ liệu người dùng để nói $ user_id = 12345, từ bảng thành viên với ID trường, đăng nhập, mật khẩu, 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 ."
";
Chúng tôi cũng có thể gọi như thế này:
parse_str[db_fields['*', $filter, $filter_by, $table_name]];
Cuộc gọi trên sẽ trả về tất cả các trường dưới dạng tên biến.
Q.
Làm thế nào để vượt qua mảng bằng tài liệu tham khảo? trong PHP?
Một
Giống như các biến bình thường, bạn có thể chuyển một mảng bằng cách tham chiếu vào một hàm bằng cách lấy tham chiếu của mảng gốc và chuyển tham chiếu đến hàm. Dưới đây là tập lệnh PHP về cách truyền mảng làm tài liệu tham khảo:
Kịch bản này sẽ in:
Before shrinking: 5,7,6,2,1,3,4,2 After shrinking: 5
Lưu ý rằng tham chiếu từng lần gọi đã bị không được chấp nhận. Bạn cần xác định các đối số là tài liệu tham khảo. Xem mẹo tiếp theo để biết chi tiết.
Xác định đối số mảng là tham chiếu trong PHP Defining an Array Argument as Reference in PHP
⇐ Truyền mảng bằng các giá trị cho các hàm trong PHP Passing Arrays by Values to Functions in PHP
⇑ Tạo các chức năng của riêng bạn trong PHP Creating Your Own Functions in PHP
Hướng dẫn PHP PHP Tutorials