How to get array intersect with single array group

和自甴很熟 提交于 2019-12-25 07:59:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!