0

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 Question:

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], ...)

1
  • Why not call_user_func_array? Commented Nov 5, 2010 at 8:41

2 Answers 2

3

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 . ')');
Sign up to request clarification or add additional context in comments.

1 Comment

Oh so eval is the trick. Understood. Thanks for the pointers.
2

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.

1 Comment

No problem :) It was just a matter of curiosity :) but thanks anyways... +1 for the solution.

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.