我们可以在 PHP 的任何函数中传递一个数组作为参数吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5459395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can we pass an array as parameter in any function in PHP?
提问by OM The Eternity
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
应该是一个数组。
回答by alex
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'));
回答by fabrik
Yes, you can safely pass an array as a parameter.
是的,您可以安全地将数组作为参数传递。
回答by Shakti Singh
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);
回答by Richard June
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);
回答by Gaurav
function sendemail(Array $id,$userid){ // forces $id must be an array
Some Process....
}
$ids = array(121,122,123);
sendmail($ids, $userId);
回答by vickirk
Its no different to any other variable, e.g.
它与任何其他变量没有什么不同,例如
function sendemail($id,$userid){
echo $arr["foo"];
}
$arr = array("foo" => "bar");
sendemail($arr, $userid);
回答by Davide Gualano
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.
请参阅类型提示。
回答by HymanWilson
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.
因此,您确实可以将数组作为参数传递。
回答by user3743829
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
我们可以在这里找到更多的数组函数
回答by Delcon
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');