从父数组获取重复值,而不是从父数组获取重复值

时间:2018-09-10 07:08:38

标签: php arrays filter unique array-filter

一个是父数组,另一个是过滤后的数组。 从那里我只想设置两个单独的数组 通用数组 唯一数组

$a = array([0]=> Array
    (
        [id] => 22429
        [user_id] => 0
        [shop_id] => 78
        [start_date] =>
        [fn_point] => 0
    ),[1]=> Array
    (
        [id] => 22430
        [user_id] => 0
        [shop_id] => 78
        [start_date] =>
        [fn_point] => 0
    ),[2]=> => Array
    (
        [id] => 22431
        [user_id] => 0
        [shop_id] => 78
        [start_date] =>
        [fn_point] => 0
    ),[3] => Array
    (
        [id] => 22432
        [user_id] => 0
        [shop_id] => 78
        [start_date] =>
        [fn_point] => 0
    ));

$b = array("0"=> Array
    (
        [id] => 22428
        [user_id] => 0
        [shop_id] => 78
        [start_date] =>
        [fn_point] => 0
    ),"1" => Array
    (
        [id] => 22430
        [user_id] => 0
        [shop_id] => 78
        [start_date] =>
        [fn_point] => 0
    ),[2]=> Array
    (
        [id] => 22431
        [user_id] => 0
        [shop_id] => 78
        [start_date] =>
        [fn_point] => 0
    ),[3] => Array
    (
        [id] => 22432
        [user_id] => 0
        [shop_id] => 78
        [start_date] =>
        [fn_point] => 0
    ));;

唯一数组的输出将为

$c = array(
        [0]=> Array
        (
            [id] => 22429
            [user_id] => 0
            [shop_id] => 78
            [start_date] =>
            [fn_point] => 0
        )
    );

公共数组的输出将为

$u = array(
        [0]=> Array
        (
            [id] => 22430
            [user_id] => 0
            [shop_id] => 78
            [start_date] =>
            [fn_point] => 0
        ),[1]=> Array
        (
            [id] => 22431
            [user_id] => 0
            [shop_id] => 78
            [start_date] =>
            [fn_point] => 0
        ),[2]=> Array
        (
            [id] => 22432
            [user_id] => 0
            [shop_id] => 78
            [start_date] =>
            [fn_point] => 0
        )
    );

3 个答案:

答案 0 :(得分:1)

对于您的通用数组:

array_intersect($a, $b);

对于您唯一的数组:

array_unique(array_merge($a, $b), SORT_REGULAR);

答案 1 :(得分:1)

Array_intersectarray_diff

$a = array('1','2','3','4');
$b = array('4','5','6','7');

$c = array_intersect($a, $b);
var_dump($c); //4

$d = array_diff($a,$b);
var_dump($d); //1,2,3

https://3v4l.org/Ut8JW

答案 2 :(得分:1)

要从这两个数组中通用,请使用array_intersect()

$c = array_values(array_intersect($a,$b)); //array_values() used for re-indexing final array
print_r($c);

输出:-https://eval.in/1055829

使用array_diff()

$c = array_values(array_diff($a,$b));//array_values() used for re-indexing final array
print_r($c);

输出:-https://eval.in/1055830

参考:-

array_values()

对于您已编辑的问题 ,请与foreach()in_array()一起使用array_column()

$final_array = [];

foreach($a as $key=>$val){
  if(in_array($val['id'],array_column($b,'id'))){
    $final_array[] = $val;
  }

}

print_r($final_array); // common values array 

输出:-https://eval.in/1055838

$final_array = [];

foreach($a as $key=>$val){
  if(!in_array($val['id'],array_column($b,'id'))){
    $final_array[] = $val;
  }

}

print_r($final_array); // non common value array

输出:-{https://eval.in/1055839