4

I'm need to send params to the function

array_intersect_key()

but sometimes i'm need to send 2 arrays, sometimes - 3 or more:

array_intersect_key($arr_1, $arr_2);
OR
array_intersect_key($arr_1, $arr_2, $arr_3, $arr_4);
1
  • If I understand your question, you've answered it already. Commented Apr 12, 2012 at 17:18

6 Answers 6

7

Assuming you want to create your own function like this, the key is to use func_get_args():

function mumble(){
    $args = func_get_args();
    foreach ($args as $arg){
        ... whatever
    }
}

If you just want to call it with multiple args, either "just do it", or use call_user_func_array():

$args = array();
... append args to $args
call_user_func_array('array_intersect_key', $args);
Sign up to request clarification or add additional context in comments.

1 Comment

I've never encountered this function before and I feel like I should have a very long time ago -- thank you so much for brining it to my attention!
0
call_user_func_array('foo', array('foo', 'bar', 'foo N'));

function foo($param1, $param2, $paramN) {
    // TADÁÁÁ
}

Comments

0

Take a look into the func_get_args() method for handling this; something like:

function my_function() {
    $numArgs=func_num_args();
    if($numArgs>0) {
        $argList=func_get_args();
        for($i=0;$i<$numArgs;$i++) {
            // Do stuff with the argument here
            $currentArg=$argList[$i];
        }
    }
}

Comments

0
function foo()
{
    $numargs = func_num_args();
    echo "Number of arguments: $numargs<br />";
    $arg_list = func_get_args();
    foreach($arg_list  as $arg) {
        if(is_array($arg)) print_r($arg)."<br />";  
        else echo "<br />".$arg;
    }
}

foo(array(1,2,3), 2, 3);

Comments

0

Please refer the http://php.net site first before asking about the standard functions, because you get all your questions answered there itself.

http://php.net/manual/en/function.array-intersect-key.php

http://php.net/manual/en/function.func-get-args.php

http://php.net/manual/en/ref.funchand.php

I got your question, here is one way you can do that :

$arr_of_arr = array($arr1, $arr2, $arr3, ..., $arrn);

or

$arr_of_arr = func_get_args();

if(count($arr_of_arr) > 1){
    for($i = 0; $i < count($arr_of_arr) - 1; $i++){
        if(! $i){
            $intersec = array_intersect_key($arr_of_arr[0], $arr_of_arr[1]);
            $i = 2;
        }
        else{
            $intersec = array_intersect_key($intersec, $arr_of_arr[$i++]);
        }
    }
}

$intersec would now contain the intersection.

2 Comments

Please expand on the links and how they relate to the question. A small example goes a long ways to increase the quality of a response.
@pst Sorry for that answer. Code now added.
-1

The array_intersect_keyhas already a protoype allowing multiple inputs :

array array_intersect_key ( array $array1 , array $array2 [, array $ ... ] )

So I don't really see the issue there.

3 Comments

The issue is "how to call it with N inputs"?
array_intersect_key ( $array1 , $array2, $array3, ...,$arrayN);
This answer does not deal with the fact that the OP will not have prior knowledge (programmatically) of how many arguments will be passed to array_intersect_key(). You don't see the issue because you misunderstand that the question is asking about writing a variadic function. Downvote, because this answer is not helpful.

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.