我们可以在PHP中的任何函数中传递一个数组作为参数吗? [英] Can we pass an array as parameter in any function in PHP?

查看:168
本文介绍了我们可以在PHP中的任何函数中传递一个数组作为参数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个向用户发送邮件的函数,我想将它的一个参数作为一个ID数组传递。



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



假设我们有一个函数:

  function sendemail($ id,$ userid){

}

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

解决方案

<你可以传递一个数组作为参数。它是按值复制的(或 COW'd ,这基本上意味着与你),所以你可以 array_pop()(以及类似的),并且不会影响任何东西。


$ b $函数sendemail($ id,$ userid){
// ...
}

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

实际上,您只能通过将其类型放入函数的参数签名来接受数组。

 函数sendemail(数组$ id,$ userid){
// ...
}

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

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


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

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.

解决方案

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.

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'));

这篇关于我们可以在PHP中的任何函数中传递一个数组作为参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆