skr07
skr07

Reputation: 725

How to get array intersect with single array group

I have array values in single array and I need to intersect the arrays inside the main array.

Here is my code:

$a[1] = array ( 'value' => 'America','value1' => 'England1','value2' => 'Australia','value3' => 'England','value4' => 'Canada', );
$a[2] = array ( 'value' => 'America','value1' => 'Wales','value2' => 'Australia','value3' => 'England1','value4' => 'Canada', );
$a[3] = array ( 'value' => 'America','value1' => 'England','value2' => 'Australia','value3' => 'England1','value4' => 'Canada', ); 

I need to show the intersect values in the array. I need the result as follows:

Array
(
    [value] => America
    [value1] => England1
    [value2] => Australia
    [value4] => Canada
)

I can't check with this array with array_intersect() function. because array keys are coming in dynamically.

This is just a sample. It goes like:

$a[1],$a[2],$a[3].....$a[n]

So please suggest any solution for this.

Upvotes: 0

Views: 558

Answers (2)

Vinie
Vinie

Reputation: 2993

Its very easy use array_intersect() as follows

$a[1] = array ( 'value' => 'America','value1' => 'England1','value2' => 'Australia','value3' => 'England','value4' => 'Canada' );
$a[2] = array ( 'value' => 'America','value1' => 'Wales','value2' => 'Australia','value3' => 'England1','value4' => 'Canada' );
$a[3] = array ( 'value' => 'America','value1' => 'England','value2' => 'Australia','value3' => 'England1','value4' => 'Canada'); 

$c=count($a);
$new=a[0];
for($i=0;$i<$c;$i++)
{
   $new=array_intersect($new, $a[$i+1]);
}

print_r($new);

Upvotes: 1

msfoster
msfoster

Reputation: 2572

You can do this with call_user_func_array:

$result = call_user_func_array("array_intersect", $a);

Upvotes: 3

Related Questions