Reputation: 11
I am having problems in PHP creating functional arguments made up of arrays separated by commas, for example I want to automate the use of the array_intersect
function.
The function accepts multiple arrays separated by comma's as its function arguments. E.g.:
array_intersect($setarray1, $setarray2, $setarray3, ...)
I have hundreds of different array's to process in the array_intersect
function (i.e I may pass 3, 4 of 5 arrays to the function at a time).
My question is, how do I create a function argument made up of comma separated arrays that I can then pass to the array_intersect
function?
Here is an example of some test data
$setsarray[]=array('all','0' ,'0&1','0&2','0&3');
$setsarray[]=array('all' ,'1' ,'0&1' ,'1&2','1&3');
$setsarray[]=array('all' ,'2' ,'0&2' ,'1&2' ,'2&3');
$setsarray[]=array('all' ,'3' ,'0&3' ,'1&3','2&3');
$setnumb=count($setsarray);
Manually I can do this...
$vennGraph['all']=array_intersect(
$setsarray[$setnumb-4],$setsarray[$setnumb-3], $setsarray[$setnumb-2]
);
The function arguments are array comma array comma array e.t.c. So I want to to generate $vennGraph['Again_and_Again']=array_intersect("put my content here");
I have been reading through the responses (thankyou!) and they sound promising but I am still dumb struck! BTW, I am using PHP Version 5.3.10
ps would it be easier if I put commas into the $setsarray as shown below?
$setsarray[]=array('all','0' ,'0&1','0&2','0&3');
$setsarray[]= ',';
$setsarray[]=array('all' ,'1' ,'0&1' ,'1&2','1&3');
$setsarray[]= ',';
$setsarray[]=array('all' ,'2' ,'0&2' ,'1&2' ,'2&3');
$setsarray[]= ',';
$setsarray[]=array('all' ,'3' ,'0&3' ,'1&3','2&3');
Upvotes: 1
Views: 955
Reputation: 522625
Your approach is wrong, you cannot concatenate arrays with commas, pass them to a function and make the function see them as separate arguments. If you write foo($bar)
, there's only one argument passed to foo
, regardless of how many commas might be in $bar
.
What you're looking for is call_user_func_array
, into which you can pass an array of arrays which will be received as separate arguments by the function. PHP 5.6+ offers some syntactic sugar around this in the form of variable length argument lists.
Upvotes: 0
Reputation: 2176
There are two methods depending on your PHP version:
Upvotes: 1