0

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.

1
  • Don't understand the result. For example how do you decide what overides what. Commented Mar 13, 2015 at 11:51

2 Answers 2

3

You can do this with call_user_func_array:

$result = call_user_func_array("array_intersect", $a);
Sign up to request clarification or add additional context in comments.

Comments

1

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);

Comments

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.