怎么取数组的交集,求大神指导
程序员文章站
2024-01-14 15:36:28
...
这是两个二维数组$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" => "赵六"));
回复讨论(解决方案)
$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);
Array( [0] => Array ( [id] => 18 [name] => 赵六 ))
可是我想要的结果是这个:
$arr1 = array( array("id" => "6","name" => "张三",), array("id" => "18","name" => "赵六"),);
是我看错了,sorry
上一篇: php 我用fwrite() 写入了一个文件,怎么文件内容全变了?
下一篇: 浅谈C#使用GDI+