0

I want to use an array in order to call a function.

Firstly, I'm getting a form via post, so I have something like this.

$var1="red";
$var2="blue";
$var3="Mary";
$varN="Ted";

I have created an array like this:

$variables=array("$var1", "$var2", "$var3", "$varN");

I want to use this array to call another function, in fact it is a MySQL query. So I want to get this result using the array:

addToTable($var1, $var2, $var3, varN);

Is it posible?

The reason I'm doing this is because I will introduce a multiple quantity of entries in the table, it's a variable number of entries and the array var names will change. For the next round I will introduce in the query something similar to this:

addToTable($var1_2, $var2_2, $var3_2, varN_2);
4
  • 1
    Your question doesn't make any sense. You're trying to send these 4 parameters $var1, $var2, $var3, varN..so what? What's the issue? What does the function look like? Commented Aug 25, 2016 at 10:10
  • I have at least 37 parameters. Everytime I add a row it multiplies. The case I exposed is just an example, if there were only 4 variables i wouldnt be doing an array. Commented Aug 25, 2016 at 10:12
  • It's an awful practice to pass so many parameters. Why don't you pass an array? Commented Aug 25, 2016 at 10:16
  • It is a waste of processing power and execution time to wrap variables in quotes, "$var1" is tersely inefficient, you just need the variable: array($var1,$var2,$varN); Commented Aug 25, 2016 at 10:17

3 Answers 3

1

You can pass it as array to the function and do a foreach inside the function.

Your variables:

$variables = array($var1, $var2, $var3, $varN);

The function:

function addToTable($array = [])
{
   foreach ($array as $key => $value) {
      // Do stuff here
   }
}

And you call the function like this:

addToTable($variables);
Sign up to request clarification or add additional context in comments.

2 Comments

But how can i do to introduce for example var1_1, var2_1, etc...?
@Ironlord, you call the function again: $variables = [$var1_1, $var2_1, $var3_1, $varN_1]; addToTable($variables);.
1

You can pass an array of arguments to a function using call_user_func_array().

function addToTable($a, $b, $c, $d)
{
    return "{$a}-{$b}-{$c}-{$d}";
}

$var1 = 'red';
$var2 = 'blue';
$var3 = 'Mary';
$varN = 'Ted';

$arguments = array($var1, $var2, $var3, $varN);
$x = call_user_func_array('addToTable', $arguments);

echo $x; // red-blue-Mary-Ted

Comments

0

In addition to call_user_func_array() already mentioned above, since 5.6 you can also perform similar actions via:

Variadic Functions

In which you can pass any number of values to a function, which collects them into an iterable argument:

function foo(...$arguments)
{
    foreach ($arguments as $arg) {
        echo $arg . PHP_EOL;
    }
}

foo(1, 2, 3);

Yields:

1
2
3

This is vaguely similar to passing in an array or call_user_func_array() - albeit more flexible - for instance, you can use typehinting:

function foo(array ...$arguments)
{
    //
}

foo([], [], []); // works, each argument is an array
foo(1, 2, 3);    // fatal error!

Argument Unpacking

As stated in the release docs Argument Unpacking does the following:

Arrays and Traversable objects can be unpacked into argument lists when calling functions by using the ... operator. This is also known as the splat operator in other languages, including Ruby.

This is useful if you already have an array of values that you want to apply to a function with an explicit argument list. This is probably the best approach since it's explicit, and therefore easiest to reason about.

function bar($a, $b, $c)
{
    echo $a . PHP_EOL;
    echo $b . PHP_EOL;
    echo $c . PHP_EOL;
}

$params = [1, 2, 3];
bar(...$params);

Yields:

1
2
3

Otherwise, if you are using <= 5.5, you could approximate the variadic approach with func_get_args():

function baz()
{
    foreach (func_get_args() as $arg) {
        echo $arg . PHP_EOL;
    }
}

baz(1, 2, 3, 4);

PHP functions do not require functions to have explicit argument lists. However, I think it is harder to reason about a function that does not state its arguments explicitly, and it's harder to validate and use the arguments passed into it.

Hope this helps :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.