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);
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);
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);
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];
}
}
}
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.
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.
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.