如何确定两个数组中是否存在给定对象?我的目的是找到添加的对象,删除的对象和更改的对象。
class Foo
{
private $id, $backup;
public $p1, $p2, $p3;
public function __construct($id, $p1, $p2, $p3){
$this->id=$id;
$this->p1=$p1;
$this->p2=$p2;
$this->p3=$p3;
$this->backup=[$p1, $p2, $p3];
}
}
$collection1=[
Foo(1,'a','aa','aaa'),
Foo(2,'b','bb','bbb'),
Foo(5,'c','cc','ccc'),
Foo(9,'d','dd','ddd'),
];
$collection2=$arr1;
$collection1[]=Foo(9,'d','dd','ddd');
unset($collection1[2]);
$collection1[0]->p2='bla'; //Will modify both $collection1 and $collection2
答案 0 :(得分:0)
来自:https://secure.php.net/manual/en/language.oop5.object-comparison.php
使用identity运算符(===)时,对象变量是相同的 当且仅当他们引用同一个类的同一个实例时。
<?php
class Foo
{
public $bar;
public function __construct($bar)
{
$this->bar = $bar;
}
}
$one = [new Foo(1),new Foo(2),new Foo(3)];
$two = $one;
var_dump($one[0] === $two[1]);
var_dump($one[0] === $two[0]);
输出:
bool(false) bool(true)