Get intersection of a multiple array in PHP

前端 未结 3 1400
有刺的猬
有刺的猬 2021-01-13 22:22

Starting Point

I have a multiple array, like the follow example.

$array = array (
  \'role_1\' => 
  array (
    0 => \'value_2\',
    1 =>         


        
3条回答
  •  不思量自难忘°
    2021-01-13 22:41

    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.

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题