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...

Acidon
  • 1,294
  • 4
  • 23
  • 44
  • what is the array_sequence_merge() and what does it do? – Siim Kallari Dec 27 '15 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](http://stackoverflow.com/questions/34482028/explain-how-this-custom-function-works-php) – Acidon Dec 27 '15 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 [mcve] here, so your question is Unclear. – mickmackusa Apr 23 '22 at 05:43

1 Answers1

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.

Tobias Xy
  • 2,039
  • 18
  • 19
  • $arguments is array consisting of parameters variables names and not the parameters itself, will it still work? – Acidon Dec 27 '15 at 19:08
  • Doesn't answer the actual question which is how to use the function with a bunch of variable variables, not an array – scrowler Dec 27 '15 at 19:15
  • 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 Dec 27 '15 at 19:23
  • @Acidon just add your arguments to an array, not a comma separated string (then pass it into the example in this answer) – scrowler Dec 27 '15 at 19:55
  • @RobbieAverill $temporary_array IS array ;) – Acidon Dec 27 '15 at 20:00
  • @RobbieAverill read comment #3 with the results of that attempt – Acidon Dec 27 '15 at 20:02
  • I suggest you set up an online example with mocked input and explain your expected output accurately – scrowler Dec 27 '15 at 20:05
  • @RobbieAverill function expects to get list of **arrays** as arguments (eg `$array1, $array2,..,$arrayn` and not just a single array, which elements are **strings** `"array1", "array2",..., "arrayn"` . Not sure if I need to make an online example to illustrate it – Acidon Dec 27 '15 at 20:12
  • What's the expected output or way of handling those arrays? Getting them in is not a problem – scrowler Dec 27 '15 at 22:29
  • I edited my answer to make more clear how it should be done. If that still does not solve your problem, I probably misunderstood your question. ;-) – Tobias Xy Dec 27 '15 at 22:51
  • @TobiasXy Hi tobias, sorry I guess my question post is confusing :(( In particular the part where I use WHILE in place of FOR.. The WHILE loop in example in question loops more then ones, each time generating different sql query and different array with results, and then I need to pass those arrays into function, each array as separate argument. I don't need to pass array elements as arguments, I need to pass multiple arrays instead. – Acidon Dec 27 '15 at 23:04
  • @RobbieAverill "Getting them in" is what this whole question is about... I beginning to think that the only way to handle my problem is to rewrite target function to intake one multidimensional array instead of multiple single-dimension arrays. Link to function is in the comment to question. – Acidon Dec 27 '15 at 23:10
  • 1
    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) – Tobias Xy Dec 27 '15 at 23:18