var_dump($this->externalProjects);
var_dump($this->projects);
$result = array_diff($this->externalProjects, $this->projects);
print_r($result);
它输出:
array (size=6) // external projects
0 =>
object(CloseCall\ValueObject\Project)[79]
private 'name' => string '(A11) Tyre Pyrolysis Plant, Civil, Mech & Elec' (length=46)
1 =>
object(CloseCall\ValueObject\Project)[80]
private 'name' => string '(B) - Millerhill New Depot Proposal' (length=35)
2 =>
object(CloseCall\ValueObject\Project)[81]
private 'name' => string '(B13) Port of Tyne Wood Pellet Handling Facilities' (length=50)
3 =>
object(CloseCall\ValueObject\Project)[82]
private 'name' => string '(C) - Millerhill Proposed New Depot' (length=35)
4 =>
object(CloseCall\ValueObject\Project)[83]
private 'name' => string '(S) - EGIP New Depot Proposal' (length=29)
5 =>
object(CloseCall\ValueObject\Project)[85]
private 'name' => string '00352 Allerton - Building fabric & structure survey' (length=51)
array (size=1) //projects
0 =>
object(CloseCall\ValueObject\Project)[15]
private 'name' => string '(A11) Tyre Pyrolysis Plant, Civil, Mech & Elec' (length=46)
// no output for array_diff
array_diff()
不适合过滤多维数据。 您的数组包含对象,因此调用 array_udiff()
并在回调中对整个对象进行 3 路比较(因为访问私有名称属性将涉及更多卷积)。 演示
namespace CloseCall\ValueObject;
class Project {
public function __construct(private string $name) {}
}
// Populate externalProjects and projects arrays
$externalProjects = [
new Project('(A11) Tyre Pyrolysis Plant, Civil, Mech & Elec'),
new Project('(B) - Millerhill New Depot Proposal'),
new Project('(B13) Port of Tyne Wood Pellet Handling Facilities'),
new Project('(C) - Millerhill Proposed New Depot'),
new Project('(S) - EGIP New Depot Proposal'),
new Project('00352 Allerton - Building fabric & structure survey')
];
$projects = [
new Project('(A11) Tyre Pyrolysis Plant, Civil, Mech & Elec')
];
var_dump(
array_udiff(
$externalProjects,
$projects,
fn($a, $b) => $a <=> $b
)
);
输出:
array(5) {
[1]=>
object(CloseCall\ValueObject\Project)#2 (1) {
["name":"CloseCall\ValueObject\Project":private]=>
string(35) "(B) - Millerhill New Depot Proposal"
}
[2]=>
object(CloseCall\ValueObject\Project)#3 (1) {
["name":"CloseCall\ValueObject\Project":private]=>
string(50) "(B13) Port of Tyne Wood Pellet Handling Facilities"
}
[3]=>
object(CloseCall\ValueObject\Project)#4 (1) {
["name":"CloseCall\ValueObject\Project":private]=>
string(35) "(C) - Millerhill Proposed New Depot"
}
[4]=>
object(CloseCall\ValueObject\Project)#5 (1) {
["name":"CloseCall\ValueObject\Project":private]=>
string(29) "(S) - EGIP New Depot Proposal"
}
[5]=>
object(CloseCall\ValueObject\Project)#6 (1) {
["name":"CloseCall\ValueObject\Project":private]=>
string(51) "00352 Allerton - Building fabric & structure survey"
}
}