Calling a function with variable number of parameters?
In regard to my question here, Jacob Relkin suggested a great solution of using call_user_func_array. That solved my problem but now I am really curious on how to do this in the absence of this function to achieve what I wanted in my original question which is below for reference:
Original Ques开发者_如何转开发tion:
I am creating an array of arrays in the following way:
$final_array = array();
for($i = 0; $i < count($elements); $i++) {
for($j = 0; $j < count($elements); $j++) {
if($i!=$j)
$final_array[] = array_intersect($elements[$i], $elements[$j]);
}
}
I am trying to find out the list of elements that occur in all the arrays inside the $final_array
variable. So I was wondering how to pass this to array_intersect
function. Can someone tell me how to construct args using $final_array[0], $final_array[1], ... $final_array[end_value]
for array_intersect
? Or if there is a better approach for this, that would be great too.
I am looking for a way to construct the following:
array_intersect($final_array[0], $final_array[1], $final_array[2], ...)
Well, the only real way to do this other than call_user_func_array
would be to implode
the resulting array into comma-separated arguments, then do something really really evil and use eval
:
$args_imploded = implode(',', $some_array);
$result = eval('return array_intersect(' . $args_imploded . ')');
Why don't you just avoid the evil eval function and use the 'call_user_func_array' function? From what I understand about your code is that the $final_array parameter is an array of array's.
$result = call_user_func_array('array_intersect', $final_array);
No need for the eval function here.
EDIT: Stupid me. I didn't read your first paragraph properly ;). Please ignore this.
精彩评论