Php 在不使用数组的情况下向函数传递不定数量的参数

Php 在不使用数组的情况下向函数传递不定数量的参数,php,function,variables,Php,Function,Variables,我的情况是,我想在php函数中传递变量。 参数的数量是不确定的。我必须在不使用数组的情况下传入函数。 就像正常的方法一样。逗号分隔 类似于测试(argum1、argum2、argum3……,…) 我将如何调用该函数?假设我有一个包含5个参数的数组(1,2,3,4,5)。我想调用func(1,2,3,4,5)这样的函数。但问题是,在调用函数时,我将如何运行参数循环。我试过func(内爆(',,数组));但它将所有返回字符串作为一个参数 在定义中,我还需要相同的格式 我可以通过数组传递可变数量的参

我的情况是,我想在php函数中传递变量。 参数的数量是不确定的。我必须在不使用数组的情况下传入函数。 就像正常的方法一样。逗号分隔

类似于
测试(argum1、argum2、argum3……,…)
我将如何调用该函数?假设我有一个包含5个参数的数组(1,2,3,4,5)。我想调用func(1,2,3,4,5)这样的函数。但问题是,在调用函数时,我将如何运行参数循环。我试过func(内爆(',,数组));但它将所有返回字符串作为一个参数

  • 在定义中,我还需要相同的格式

  • 我可以通过数组传递可变数量的参数,但我必须传递逗号分隔的参数

  • 我必须通过逗号分隔。但在传递时,我不知道参数的数量,它们在数组中


您可以将函数定义为

function test ()
然后在php中使用
func\u get\u args
函数

然后可以将参数作为数组处理

范例

function reverseConcat(){
      return implode (" ", array_reverse(func_get_args()));
}

echo reverseConcat("World", "Hello"); // echos Hello World
如果您真的想像处理命名参数一样处理它们,您可以这样做

function getDistance(){
     $params = array("x1", "y1", "x2", "y2");
     $args = func_get_args();
     // trim excess params
     if (count($args) >  count($params) {
         $args = array_slice(0, count($params));
     } elseif (count($args) < count($params)){
         // define missing parameters as empty string
         $args = array_pad($args, count($params), "");
     } 
     extract (array_combine($params, $args));
     return sqrt(pow(abs($x1-$x2),2) + pow(abs($y1-$y2),2));

}
函数getDistance(){ $params=数组(“x1”、“y1”、“x2”、“y2”); $args=func_get_args(); //修剪多余参数 if(count($args)>count($params){ $args=array_slice(0,count($params)); }elseif(count($args)
您可以使用:

$function_args = func_get_args();
在test()函数定义中。

使用此函数:

  function test() {
      $args = func_get_args();
      foreach ($args as $arg) {
        echo "Arg: $arg\n";
      }
    }

也可以使用“分解”来使用数组

但要小心,$argum1、$argum2、等在值中不应包含。您可以通过添加任何分隔符来克服这一问题。$separator=“VeryUniqueSeparator” 我没有代码,所以无法说出准确的代码。但是,按照您的要求处理这些代码。

在呼叫端,使用。
在函数内部,使用


由于这种方法只是将一个数组转化为一个数组,我怀疑这种方法是否明智。如果调用接收时有未知数量的参数,则这两个函数都可以。如果两端都是动态的,为什么不直接传递数组?!我不确定你的意思“相同格式”。你是说相同的类型,就像它们都必须是字符串一样?还是说它们都必须满足某些标准,比如如果是电话号码列表,它们必须是
(ddd)ddd ddddd

如果是后者,您在使用预定义参数时也会遇到同样多的问题,因此我假设您的意思是希望它们都是相同的类型

因此,除了已经建议的使用
func\u get\u args()
的解决方案之外,我还将应用
array\u filter()
,以确保类型:

function set_names() {

    function string_only($arg) {

        return(is_string($arg));

    }

    $names_provided = func_get_args();  
    // Now you have an array of the args provided

    $names_provided_clean = array_filter($names_provided, "string_only"); 
    // This pulls out any non-string args

    $names = array_values($names_provided_clean); 
    // Because array_filter doesn't reindex, this will reset numbering for array.

     foreach($names as $name) {
           echo $name;
           echo PHP_EOL;
      }
}

set_names("Joe", "Bill", 45, array(1,2,3), "Jane");

请注意,我没有做任何更深入的健全性检查,因此如果没有传入值,可能会出现问题,等等。

RTLM:是的,我已经讲过了,但我的问题是如何传递变量?如果我不知道参数的数量,如果它们在不定数组中。你可以简单地通过逗号分隔来传递。如何,我将通过分隔来传递逗号。在运行时..谢谢@Orangepill,但是,我将如何调用该函数?假设我有一个包含5个参数的数组(1,2,3,4,5)。我想像func(1,2,3,4,5)一样调用该函数。但问题是,在调用该函数时,我将如何运行参数循环。我尝试了func(内爆(',,array));但它将所有返回字符串作为一个参数。.这样做的方法是使用
call\u user\u func\u数组(“reverseConcat”,$array);
Wow:)谢谢。这就是我正在搜索的call_user_func_arrayI正在创建一个PHP框架,为了方便用户,我将参数放入函数it self:)
function set_names() {

    function string_only($arg) {

        return(is_string($arg));

    }

    $names_provided = func_get_args();  
    // Now you have an array of the args provided

    $names_provided_clean = array_filter($names_provided, "string_only"); 
    // This pulls out any non-string args

    $names = array_values($names_provided_clean); 
    // Because array_filter doesn't reindex, this will reset numbering for array.

     foreach($names as $name) {
           echo $name;
           echo PHP_EOL;
      }
}

set_names("Joe", "Bill", 45, array(1,2,3), "Jane");