一个是父数组,另一个是过滤后的数组。 从那里我只想设置两个单独的数组 通用数组 唯一数组
$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
)
);
答案 0 :(得分:1)
对于您的通用数组:
array_intersect($a, $b);
对于您唯一的数组:
array_unique(array_merge($a, $b), SORT_REGULAR);
答案 1 :(得分:1)
$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
答案 2 :(得分:1)
要从这两个数组中通用,请使用array_intersect()
$c = array_values(array_intersect($a,$b)); //array_values() used for re-indexing final array
print_r($c);
$c = array_values(array_diff($a,$b));//array_values() used for re-indexing final array
print_r($c);
参考:-
对于您已编辑的问题 ,请与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
和
$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