9

Starting Point

I have a multidimensional 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 a 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.

Attempt

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.

2
  • $temp = $array[0]; for ($i = 1; $i < count($array) -1; $i++) { $temp = array_intersect($temp, $array[$i]); } Commented Jun 27, 2016 at 14:15
  • Thanks for your fast hint, but this is always NULL in my tests. Commented Jun 27, 2016 at 14:18

4 Answers 4

12

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.

Sign up to request clarification or add additional context in comments.

1 Comment

array_intersect() does not accept unknown named parameters - PHP Version 8
7

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.

4 Comments

The result of this is always NULL. I had tried this.
@bueltge Have you also tried with call_user_func_array?
Yes, sorry, it should have been call_user_func_array.
For the protocol: at first the answer was with the function call_user_func. The switch to call_user_func_array is really helpful. Thanks in advice!
1

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 ) 

1 Comment

I'd assume the number of roles is unknown, so this call style is a bit impractical.
0

As of PHP 5.6+ the splat/spread operator can be used to unpack the arguments in a call to array_intersect(). In order to remove the non-numeric keys from the array, pass $array to a call to array_values().

$result = array_intersect(...array_values($array));

See a demonstration in this playground example.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.