Home > Backend Development > PHP Tutorial > 怎么取数组的交集,求大神指导

怎么取数组的交集,求大神指导

WBOY
Release: 2016-06-20 12:36:43
Original
1103 people have browsed it

这是两个二维数组$arr,$arr1,怎么取出相同的交集$arr = array(		array("id" => "3","name" => "张三"),		array("id" => "6","name" => "李四"),		array("id" => "8","name" =>  "王五"),		array("id" => "18","name" => "赵六"),	 	array("id" => "20","name" => "七七")	 );$arr1 = array(		array("id" => "6","name" => "张三",),                array("id" => "18","name" => "赵六"),);想要的结果是array(		array("id" => "6","name" => "张三",),                array("id" => "18","name" => "赵六"));
Copy after login


回复讨论(解决方案)

$arr = array(        array("id" => "3","name" => "张三"),        array("id" => "6","name" => "李四"),        array("id" => "8","name" =>  "王五"),        array("id" => "18","name" => "赵六"),        array("id" => "20","name" => "七七")     );$arr1 = array(        array("id" => "6","name" => "张三",),        array("id" => "18","name" => "赵六"),);foreach($arr as $a)  foreach($arr1 as $k=>$b)    if($a == $b) {      $r[] = $b;      unset($arr1[$k]);    }print_r($r);
Copy after login
Array(    [0] => Array        (            [id] => 18            [name] => 赵六        ))
Copy after login

可是我想要的结果是这个:

$arr1 = array(        array("id" => "6","name" => "张三",),        array("id" => "18","name" => "赵六"),);
Copy after login

是我看错了,sorry

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template