Php 从2个不同的数组中删除相似值后获取结果数组
有2组代码,每个代码给出2个不同的数组 第一盘是这个Php 从2个不同的数组中删除相似值后获取结果数组,php,arrays,object,multidimensional-array,Php,Arrays,Object,Multidimensional Array,有2组代码,每个代码给出2个不同的数组 第一盘是这个 $this->db->select('id'); $query = $this->db->get('user'); echo "<pre>"; print_r( $query->result()); echo "</pre>"; 第二个是这个 Array ( [0] => stdClass Object ( [user_id] =&g
$this->db->select('id');
$query = $this->db->get('user');
echo "<pre>";
print_r( $query->result());
echo "</pre>";
第二个是这个
Array
(
[0] => stdClass Object
(
[user_id] => 1
)
)
我希望得到不应该有重复值的最终数组,例如在上面的2个数组中,数组1在两个数组中都是公共的,因此它不应该在那里,在生成的数组中,我应该只得到值2。您可以采取以下方法。从第二个数组中列出ID。然后使用该列表筛选第一个。如果某个id不在该列表中:保留它
$this->db->select('user_id');
$query_two = $this->db->get('request_user');
echo "<pre>";
print_r( $query_two->result());
echo "</pre>";
Array
(
[0] => stdClass Object
(
[user_id] => 1
)
)
<?php
$one = [
(object) ['id' => 1],
(object) ['id' => 2],
];
$two = [
(object) ['id' => 1],
];
$ids_in_two = [];
foreach($two as $val) {
$ids_in_two[] = $val->id;
}
$filtered = array_filter($one, function($val) use ($ids_in_two) {
return !in_array($val->id, $ids_in_two);
});
var_dump($filtered);
array(1) {
[0]=>
object(stdClass)#1 (1) {
["id"]=>
int(2)
}
}