简体   繁体   English

PHP如何使用数组中的值作为函数参数

[英]php how to use values from array as function parameters

I have problems using a variable amount of array values as parameters for my function. 我在使用可变数量的数组值作为函数的参数时遇到问题。 Since PHP has the functions "func_num_args()" (amount of function parameters) and "func_get_args()" (saves the parameters in an array) for variable amounts of parameters, it would be great to use the values of an array as parameters. 由于PHP具有函数“ func_num_args()”(函数参数的数量)和“ func_get_args()”(将参数保存在数组中)用于可变数量的参数,因此最好将数组的值用作参数。

Like a function that adds all integer values: 就像一个将所有整数值相加的函数:

function add()
{
   $amount = func_num_args();
   $param = func_get_args();

   $sum = 0;


    for ($i=0; $i < $amount; $i++)
    {
       $sum = $sum + $param[$i];
    }
    echo "$sum";
}

Now I would define the array with the values which should be added and run the function: 现在,我将使用应添加的值定义数组并运行该函数:

$values = array(1,2,3,4,5,6,7,24,2345,6323,765,234);

add($values);

But when I run the function as shown, I get an error, since only the first value of the array $values was send over and not all the values. 但是,当我如图所示运行函数时,出现错误,因为只发送了数组$ values的第一个值,而不发送所有值。

How do I make sure all values are given as parameters??? 如何确保所有值都作为参数给出???

If I run the function as follows, it works. 如果我按以下方式运行该函数,则它将起作用。 But than I need to know the precise amount of parameters which need to be passed on to the function. 但是比我需要知道需要传递给函数的精确参数数量更重要。 Here it is just 12, but what to do if the user enters something like 450 parameters ... 这里只有12,但是如果用户输入450参数,该怎么办...

add(1,2,3,4,5,6,7,24,2345,6323,765,234)

add($values[0],$values[1], ...)

There is another solution when using "global $values" in the function, where the variable is made available inside the function. 在函数中使用“全局$ values”时,还有另一种解决方案,其中变量在函数内部可用。 But this solutions is not nice and has its problems. 但是这种解决方案不是很好,并且有问题。

And when running the same function again with global variables I would need to change the function , set to the new parameter set (like $values2). 当再次使用全局变量运行相同的函数时,我需要更改函数,将其设置为新的参数集(例如$ values2)。

And again, the functions for "variable amounts of parameters" (func_num_args() and func_get_args() ) would be useless, if you cannot pass variable amounts of parameters to the function by an array. 同样,如果您不能通过数组将可变数量的参数传递给函数,则“可变数量的参数”的函数(func_num_args()和func_get_args())将无用。

Help would be appreciated. 帮助将不胜感激。 Greetings 问候

You are confusing variable arguments and using an array of values as an argument. 您在混淆变量参数,并使用值数组作为参数。 In the example you post, $param is is not equal to 在您发布的示例中, $param不等于

array(1,2,3,4,5,6,7,24,2345,6323,765,234)

it is equal to 它等于

array(array(1,2,3,4,5,6,7,24,2345,6323,765,234))

that is, you are calling 也就是说,你在打电话

sum(array(1,2,3,4,5,6,7,24,2345,6323,765,234));

If you want to call 如果你想打电话

sum(1,2,3,4,5,6,7,24,2345,6323,765,234);

then you can use call_user_func_array : 然后您可以使用call_user_func_array

call_user_func_array('sum', $values);

If you don't want to use func_num_args() and func_get_args() then try this : 如果您不想使用func_num_args()func_get_args()尝试以下操作:

<?php
function add($values)
{
   $sum=0;
    for ($i=0; $i < count($values); $i++)
    {
      $sum = $sum + $values[$i];
    }
    return $sum;
}
$values = array(1,2,3,4,5,6,7,24,2345,6323,765,234);

echo add($values);
?>

You are confusing passing an array as (one single) parameter, and accessing multiple parameters as an array inside the function. 数组作为(单个)参数传递给函数,以及将多个参数作为数组内部的函数来访问,这令人困惑。

foo(1, 2, 3); // three parameters
// vs
foo({1, 2, 3}); // one array as parameter, containing three values

And your function needs the } after the $sum = 0; $sum = 0;之后,您的函数需要使用} $sum = 0; removed and placed at the very end, then it will work: 移除并放置在最末端,那么它将起作用:

function add() {
   $amount = func_num_args();
   $param = func_get_args();

   $sum = 0;

   for ($i=0; $i < $amount; $i++) {
      $sum = $sum + $param[$i];
   }
   return $sum; // functions in general should not “echo out” results,
                // but rather return them
}

echo add(1,2,3); // results in 6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM