Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get intersection of a multi-dimensional array in PHP

Starting Point

I have a multi-dimensional array, like the follow example:

$array = array (
  'role_1' => 
  array (
    0 => 'value_2',
    1 => 'value_3',
  ),
  'role_2' => 
  array (
    0 => 'value_1',
    1 => 'value_2',
  ),
  'role_3' => 
  array (
    0 => 'value_2',
    1 => 'value_3',
  ),
)

Goal

I like to loop about the sub-arrays to get only the intersection. The array was created dynamically, can have a lot of sub-arrays role_[x] and also a lot of key/value inside the sub-arrays. The key is not necessary, only the value. The key is also a count, not a string.

As result I like to get in this example this small array.

$array = array( 'value_2' )

The index, "the array-name", like role_1 of the sub-arrays is not more relevant after intersection. Important for me in the result is the values, only the values there are existed in each sub-array.

Try

I had tried with the source, but I think it is possible much simpler.

$value_stack = array();
$result = array();
$i = 0;
foreach( $settings_ as $role => $values ) {

    foreach( $values as $value ){

        if( in_array( $value,$value_stack ) || $i === 0 ) {
            $result[ $role ][] = $value;
        }

        $value_stack[] = $value;
    }
    $i++;

};

The merge of this multi array result should run with a array_merge in a loop.

Thanks for your time.

like image 400
bueltge Avatar asked Jun 27 '16 14:06

bueltge


People also ask

How can I find the intersection of two arrays in PHP?

The array_intersect() function compares the values of two (or more) arrays, and returns the matches. This function compares the values of two or more arrays, and return an array that contains the entries from array1 that are present in array2, array3, etc.

What does array_intersect return?

array_intersect() returns an array containing all the values of array that are present in all the arguments.

How do I check if two arrays have the same element in PHP?

Now, to check whether two arrays are equal or not, an iteration can be done over the arrays and check whether for each index the value associated with the index in both the arrays is the same or not. PHP has an inbuilt array operator( === ) to check the same but here the order of array elements is not important.

What is a multi dimensional array in PHP example?

A multidimensional array is an array containing one or more arrays. PHP supports multidimensional arrays that are two, three, four, five, or more levels deep. However, arrays more than three levels deep are hard to manage for most people.


3 Answers

You can use array_intersect to cover the dynamic $data as such:

$data = array (
  'role_1' => 
  array (
    0 => 'value_2',
    1 => 'value_3',
  ),
  'role_2' => 
  array (
    0 => 'value_1',
    1 => 'value_2',
  ),
  'role_3' => 
  array (
    0 => 'value_2',
    1 => 'value_3',
  )
);

$result = call_user_func_array('array_intersect', $data);

call_user_func_array will help spread the elements of your array as parameters inside array_intersect.

like image 200
Sunny Patel Avatar answered Oct 18 '22 14:10

Sunny Patel


You should be able to do

call_user_func_array('array_intersect', $array_of_arrays)

This will pass each element of your array of arrays as an argument to array_intersect, which takes a variable number of arrays as arguments and returns their intersection.

like image 7
Rose Kunkel Avatar answered Oct 18 '22 14:10

Rose Kunkel


array_intersect work for this:

$data = array (
  'role_1' => 
  array (
    0 => 'value_2',
    1 => 'value_3',
  ),
  'role_2' => 
  array (
    0 => 'value_1',
    1 => 'value_2',
  ),
  'role_3' => 
  array (
    0 => 'value_2',
    1 => 'value_3',
  )
);


$result = array_intersect($data['role_1'], $data['role_2'], $data['role_3']);
print_r($result);

result :

Array ( [0] => value_2 ) 
like image 1
miglio Avatar answered Oct 18 '22 14:10

miglio