我们可以在PHP的任何函数中传递数组作为参数吗?

时间:2021-08-16 17:29:36

I have a function to send mail to users and I want to pass one of its parameter as an array of ids.

我有一个向用户发送邮件的功能,我希望将其中一个参数作为id数组传递。

Is this possible to do? If yes, how can it be done?

这可能吗?如果是,怎么办?

Suppose we have a function as:

假设我们有一个函数:

function sendemail($id, $userid) {

}

In the example, $id should be an array.

在示例中,$ id应该是一个数组。

11 个解决方案

#1


31  

You can pass an array as an argument. It is copied by value (or COW'd, which essentially means the same to you), so you can array_pop() (and similar) all you like on it and won't affect anything outside.

您可以将数组作为参数传递。它是按值复制的(或者COW'd,这实际上对你来说意味着相同),所以你可以在array_pop()(和类似的)上喜欢它,并且不会影响外面的任何东西。

function sendemail($id, $userid){
    // ...
}

sendemail(array('a', 'b', 'c'), 10);

You can in fact only accept an array there by placing its type in the function's argument signature...

事实上你只能通过将其类型放在函数的参数签名中来接受数组...

function sendemail(array $id, $userid){
    // ...
}

You can also call the function with its arguments as an array...

您也可以将其参数作为数组调用该函数...

call_user_func_array('sendemail', array('argument1', 'argument2'));

#2


5  

Yes, you can safely pass an array as a parameter.

是的,您可以安全地将数组作为参数传递。

#3


4  

What should be clarified here.

这里应该澄清什么。

Just pass the array when you call this function.

只需在调用此函数时传递数组。

function sendemail($id,$userid){
Some Process....
}
$id=array(1,2);
sendmail($id,$userid);

#4


4  

Yes, you can do that.

是的,你可以这么做。

function sendemail($id_list,$userid){
    foreach($id_list as $id) {
        printf("$id\n"); // Will run twice, once outputting id1, then id2
    }
}

$idl = Array("id1", "id2");
$uid = "userID";
sendemail($idl, $uid);

#5


3  

function sendemail(Array $id,$userid){  // forces $id must be an array
Some Process....
}


$ids  = array(121,122,123);
sendmail($ids, $userId);

#6


2  

Its no different to any other variable, e.g.

它与任何其他变量没有区别,例如

function sendemail($id,$userid){
  echo $arr["foo"]; 
}

$arr = array("foo" => "bar");
sendemail($arr, $userid);

#7


1  

In php 5, you can also hint the type of the passed variable:

在php 5中,您还可以提示传递的变量的类型:

function sendemail(array $id, $userid){
  //function body
}

See type hinting.

请参阅类型提示。

#8


1  

Since PHP is dynamically weakly typed, you can pass any variable to the function and the function will try to do its best with it.

由于PHP是动态弱类型的,因此您可以将任何变量传递给函数,函数将尽力使用它。

Therefore, you can indeed pass arrays as parameters.

因此,您确实可以将数组作为参数传递。

#9


0  

Yes, we can pass arrays to a function.

是的,我们可以将数组传递给函数。

$arr = array(“a” => “first”, “b” => “second”, “c” => “third”);

function user_defined($item, $key)
{
    echo $key.”-”.$item.”<br/>”;
} 

array_walk($arr, ‘user_defined’);

We can find more array functions here

我们可以在这里找到更多数组函数

http://skillrow.com/array-functions-in-php-part1/

http://skillrow.com/array-functions-in-php-part1/

#10


0  

even more cool, you can pass a variable count of parameters to a function like this:

更酷的是,你可以将一个可变数量的参数传递给这样的函数:

function sendmail(...$users){
   foreach($users as $user){

   }
}

sendmail('user1','user2','user3');

#11


0  

<?php

function takes_array($input)

{

    echo "$input[0] + $input[1] = ", $input[0]+$input[1];

}

?>

#1


31  

You can pass an array as an argument. It is copied by value (or COW'd, which essentially means the same to you), so you can array_pop() (and similar) all you like on it and won't affect anything outside.

您可以将数组作为参数传递。它是按值复制的(或者COW'd,这实际上对你来说意味着相同),所以你可以在array_pop()(和类似的)上喜欢它,并且不会影响外面的任何东西。

function sendemail($id, $userid){
    // ...
}

sendemail(array('a', 'b', 'c'), 10);

You can in fact only accept an array there by placing its type in the function's argument signature...

事实上你只能通过将其类型放在函数的参数签名中来接受数组...

function sendemail(array $id, $userid){
    // ...
}

You can also call the function with its arguments as an array...

您也可以将其参数作为数组调用该函数...

call_user_func_array('sendemail', array('argument1', 'argument2'));

#2


5  

Yes, you can safely pass an array as a parameter.

是的,您可以安全地将数组作为参数传递。

#3


4  

What should be clarified here.

这里应该澄清什么。

Just pass the array when you call this function.

只需在调用此函数时传递数组。

function sendemail($id,$userid){
Some Process....
}
$id=array(1,2);
sendmail($id,$userid);

#4


4  

Yes, you can do that.

是的,你可以这么做。

function sendemail($id_list,$userid){
    foreach($id_list as $id) {
        printf("$id\n"); // Will run twice, once outputting id1, then id2
    }
}

$idl = Array("id1", "id2");
$uid = "userID";
sendemail($idl, $uid);

#5


3  

function sendemail(Array $id,$userid){  // forces $id must be an array
Some Process....
}


$ids  = array(121,122,123);
sendmail($ids, $userId);

#6


2  

Its no different to any other variable, e.g.

它与任何其他变量没有区别,例如

function sendemail($id,$userid){
  echo $arr["foo"]; 
}

$arr = array("foo" => "bar");
sendemail($arr, $userid);

#7


1  

In php 5, you can also hint the type of the passed variable:

在php 5中,您还可以提示传递的变量的类型:

function sendemail(array $id, $userid){
  //function body
}

See type hinting.

请参阅类型提示。

#8


1  

Since PHP is dynamically weakly typed, you can pass any variable to the function and the function will try to do its best with it.

由于PHP是动态弱类型的,因此您可以将任何变量传递给函数,函数将尽力使用它。

Therefore, you can indeed pass arrays as parameters.

因此,您确实可以将数组作为参数传递。

#9


0  

Yes, we can pass arrays to a function.

是的,我们可以将数组传递给函数。

$arr = array(“a” => “first”, “b” => “second”, “c” => “third”);

function user_defined($item, $key)
{
    echo $key.”-”.$item.”<br/>”;
} 

array_walk($arr, ‘user_defined’);

We can find more array functions here

我们可以在这里找到更多数组函数

http://skillrow.com/array-functions-in-php-part1/

http://skillrow.com/array-functions-in-php-part1/

#10


0  

even more cool, you can pass a variable count of parameters to a function like this:

更酷的是,你可以将一个可变数量的参数传递给这样的函数:

function sendmail(...$users){
   foreach($users as $user){

   }
}

sendmail('user1','user2','user3');

#11


0  

<?php

function takes_array($input)

{

    echo "$input[0] + $input[1] = ", $input[0]+$input[1];

}

?>