问题
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:
You can do this with call_user_func_array
:
$result = call_user_func_array("array_intersect", $a);
回答2:
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);
来源:https://stackoverflow.com/questions/29031430/how-to-get-array-intersect-with-single-array-group