Skip to content

Core\ObjectMap::keys

$objectMap->keys(): array<K>

Returns every key of a Core\ObjectMap as a list. The keys are in the order they were added, which is the same order a foreach over the map gives. This replaces a foreach over an SplObjectStorage that collects each object into an array.

The list is a new array. If you change the map after the call, the list does not change. If the map is empty, the result is an empty array.

The key at each position belongs to the value at the same position in the list that values returns.

The examples show listing the keys, the list staying the same when the map changes, and a common use: printing the names of the users who are online.

Return value

Returns array<K>. A new array<K> list. It is empty for an empty map. Each position matches the same position in values().

Examples

List the keys

<?nvs
// Shows how to get all the keys of a map as a list.
final class City
{
public string $name;
public function constructor(string $name)
{
$this->name = $name;
}
}
Core\ObjectMap<City, int> $people = new Core\ObjectMap<City, int>();
$people->set(new City('Lyon'), 522000);
$people->set(new City('Graz'), 298000);
$people->set(new City('Turku'), 201000);
// `keys` returns the three cities in the order they were added.
array<City> $cities = $people->keys();
// This prints 3, then "Lyon", "Graz" and "Turku".
echo Core\Arr::count($cities), "\n";
foreach ($cities as City $city) {
echo $city->name, "\n";
}
Output
3
Lyon
Graz
Turku

The list is a copy

<?nvs
// Shows that the list from `keys` does not change when the map changes later.
final class Seat
{
public string $label;
public function constructor(string $label)
{
$this->label = $label;
}
}
Seat $a1 = new Seat('A1');
Seat $a2 = new Seat('A2');
Core\ObjectMap<Seat, string> $booked = new Core\ObjectMap<Seat, string>();
$booked->set($a1, 'Mia');
$booked->set($a2, 'Tom');
array<Seat> $before = $booked->keys();
// Tom cancels, so the map now has one entry.
$booked->remove($a2);
// The list was made before the change. This prints 2 and 1.
echo Core\Arr::count($before), "\n";
echo Core\Arr::count($booked->keys()), "\n";
// An empty map gives an empty list. This prints 0.
$booked->clear();
echo Core\Arr::count($booked->keys()), "\n";
Output
2
1
0

List users who are online

<?nvs
// Shows how to print the names of the users who are online.
final class User
{
public string $name;
public function constructor(string $name)
{
$this->name = $name;
}
}
// The map stores the time of each user's last action.
Core\ObjectMap<User, string> $online = new Core\ObjectMap<User, string>();
$online->set(new User('ana'), '10:02');
$online->set(new User('li'), '10:04');
$online->set(new User('sam'), '10:05');
// `keys` gives the users as a list, and `Core\Arr::map` turns each one into its name.
array<string> $names = Core\Arr::map($online->keys(), fn(User $user): string => $user->name);
// This prints "online: ana, li, sam".
echo 'online: ', Core\Str::join($names, ', '), "\n";
Output
online: ana, li, sam