0

I have a custom function array_sequence_merge(). With the help of func_get_args(), any number of arguments can be passed to the function as long as they are all ARRAYS.

The problem is, the arrays that need to be passed into function are created dynamically with WHILE loop of unknown size:

while($n < count($site)) {
    $siten = $site[$n];
    $sql = "SELECT url FROM `".$$siten->domain."`";
    $result = $con->query($sql);
    while($row = $result->fetch_array()){
        ${$siten."_url_list"} = $row['url'];
    }
}

So. for example, if there 3 elements in $site array, the resulting 3 arrays will be:

$element1_url_list
$element2_url_list
$element3_url_list

Now they have to be passed to array_sequence_merge() function to get:

array_sequence_merge($element1_url_list, $element2_url_list, $element3_url_list);

Each time the amount of elements in $site array is different, and so is the amount of dynamically created XXXXX_url_list arrays.

Question: How do I pass those array variables into the function?

The way I approached it, was to store dynamically created array variable name in a temporary array right after it is created in a WHILE loop. But I am not sure what to with it next... If only I could do some kind of "argument list concatenation", something like:

$arguments = $temporary_array[0];
while($n < count($temporary_array)) {
    $arguments .= ",".$temporary_array[$n];
}

array_sequence_merge($arguments);

Just not sure how to do it right...

3
  • what is the array_sequence_merge() and what does it do? Commented Dec 27, 2015 at 18:45
  • it only accepts one-dimensional arrays as arguments and passing a single multi-dimensional array as an argument will not work. Here is the link to function Commented Dec 27, 2015 at 18:56
  • $n is never incremented and $site is never altered, so your snippet will suffer from an infinite loop. We do not have a minimal reproducible example here, so your question is Unclear. Commented Apr 23, 2022 at 5:43

1 Answer 1

2

Use call_user_func_array like this:

call_user_func_array('array_sequence_merge', $arguments);

with $arguments being your array of parameters.

Then every element of the array is handled as a separate parameter of "array_sequence_merge".

Edit To achieve something like

array_sequence_merge($element1_url_list, $element2_url_list, $element3_url_list, ...);

you have to change the code like this:

$arguments = [];
while($n < count($site)) {
    $siten = $site[$n];
    $sql = "SELECT url FROM `".$$siten->domain."`";
    $result = $con->query($sql);
    while($row = $result->fetch_array()){
        $arguments[] = $row['url'];
    }
}

// $result contains the return value of the array_sequence_merge function call
$result = call_user_func_array('array_sequence_merge', $arguments);

So basically instead of creating $elementN_url_list variables write them into the array $arguments and pass it to call_user_func_array as second parameter.

Sign up to request clarification or add additional context in comments.

13 Comments

$arguments is array consisting of parameters variables names and not the parameters itself, will it still work?
Doesn't answer the actual question which is how to use the function with a bunch of variable variables, not an array
I just tried it and it only passes string values of $temporary_array (or $arguments in your answer) elements and not the variables of arrays that needed.
@Acidon just add your arguments to an array, not a comma separated string (then pass it into the example in this answer)
But that is exactly what call_user_func_array does (not to be confused with "call_user_func"!!). It takes a single array as second argument and passes every element of that array as a single argument. So what you have to do is to create one twodimensional array of all your parameters and pass it to call_user_func_array, which will pass multiple single-dimensional arrays as separate parameters to you function. Also check the documentation of this function (see link in the first line of my answer)
|

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.