Skip to content

Core\ObjectSet::intersection

$objectSet->intersection(Core\ObjectSet $other): Core\ObjectSet$objectSet->intersection(
    Core\ObjectSet $other,
): Core\ObjectSet

Builds a new Core\ObjectSet with only the values that are in this set and also in $other. This replaces a loop over one SplObjectStorage that checks contains on another one.

A value matches when it is == to a value in the other set. For an object of your own class, that is only the same object. Two different objects with equal property values do not match, so neither is in the result. Two Date values for the same day do match.

The result is a new set. intersection does not change this set or $other. The values keep the order they have in this set. If the two sets have no value in common, the result is an empty set.

The examples show the values two sets share, and two sets that share nothing. The last one shows a common use: finding the users who are online and are also allowed to receive an alert.

Parameters

  • $otherCore\ObjectSetThe other set.

Return value

Returns Core\ObjectSet. A new Core\ObjectSet<T>, in the order of this set. It is empty when the two sets have no value in common. Neither set is changed.

Changelog

VersionChange
0.0.6Core\ObjectSet->intersection() and difference() return a new set. The old names were intersect and diff.

Examples

Values in both sets

<?nvs
// Shows how to find the values that two sets have in common.
final class Tag
{
public string $name;
public function constructor(string $name)
{
$this->name = $name;
}
}
Tag $red = new Tag('red');
Tag $blue = new Tag('blue');
Tag $green = new Tag('green');
Core\ObjectSet<Tag> $shirt = new Core\ObjectSet<Tag>();
$shirt->add($red);
$shirt->add($blue);
$shirt->add($green);
Core\ObjectSet<Tag> $scarf = new Core\ObjectSet<Tag>();
$scarf->add($green);
$scarf->add($red);
// `intersection` returns a new set. The values keep the order they have in `$shirt`.
Core\ObjectSet<Tag> $shared = $shirt->intersection($scarf);
// This prints red and green, one on each line.
foreach ($shared as Tag $tag) {
echo $tag->name, "\n";
}
// `$shirt` and `$scarf` do not change. This prints their sizes, "3 2".
echo $shirt->count(), ' ', $scarf->count(), "\n";
Output
red
green
3 2

Nothing in common

<?nvs
// Shows the result when two sets have no value in common.
final class Tag
{
public string $name;
public function constructor(string $name)
{
$this->name = $name;
}
}
Core\ObjectSet<Tag> $first = new Core\ObjectSet<Tag>();
$first->add(new Tag('sale'));
Core\ObjectSet<Tag> $second = new Core\ObjectSet<Tag>();
$second->add(new Tag('sale'));
// The two "sale" tags have equal property values, but they are two different objects.
// So the sets share nothing, and the result is an empty set.
Core\ObjectSet<Tag> $shared = $first->intersection($second);
// This prints 0.
echo $shared->count(), "\n";
// This prints "empty".
echo $shared->isEmpty() ? 'empty' : 'not empty', "\n";
Output
0
empty

Online users to alert

<?nvs
// Shows how to find the users who are online and are also allowed to receive an alert.
final class User
{
public string $name;
public function constructor(string $name)
{
$this->name = $name;
}
}
User $ana = new User('Ana');
User $li = new User('Li');
User $sam = new User('Sam');
User $kim = new User('Kim');
Core\ObjectSet<User> $admins = new Core\ObjectSet<User>();
$admins->add($ana);
$admins->add($sam);
$admins->add($kim);
Core\ObjectSet<User> $online = new Core\ObjectSet<User>();
$online->add($li);
$online->add($kim);
$online->add($ana);
// Only admins receive the alert, and only if they are online now.
// This prints "Alert Ana" and "Alert Kim". Sam is offline and Li is not an admin.
foreach ($admins->intersection($online) as User $user) {
echo 'Alert ', $user->name, "\n";
}
Output
Alert Ana
Alert Kim