Skip to content

KeyedIterable

The interface a class implements so that a foreach loop can give a key and a value for each entry.

A KeyedIterable<K, V> has one method, iterateKeyed. It returns a KeyedIterator<K, V>, a cursor that moves through the entries one at a time. You write the types when you declare the class, for example implements KeyedIterable<string, int>. Every loop over the object then gets a string key and an int value. A class without type arguments does not compile.

Write foreach ($sheet as var $day => var $hours). The loop calls iterateKeyed once at the start, so each loop gets a new cursor. You can walk the same object twice, and you can nest two loops over it.

A loop with only a value variable also works, and it gives the values. A class can also implement Iterable. The method is called iterateKeyed, not iterate, so one class can have both methods. Then a loop with one variable uses iterate. Core\ObjectMap is such a class: a loop with one variable gives its keys.

Good to know: if the class does not have the iterateKeyed method, the error is E0449.

Examples

A timesheet you can walk twice

<?nvs
// A weekly timesheet that a `foreach` loop walks with a key and a value, as often as you like.
// The class says `implements KeyedIterable<string, int>`. The key is the day, and the value is the hours.
class TimesheetCursor implements KeyedIterator<string, int>
{
private array<string> $days;
private array<int> $hours;
private int $at = -1;
public function constructor(array<string> $days, array<int> $hours)
{
$this->days = $days;
$this->hours = $hours;
}
// The cursor's `advance` moves to the next day. It returns `false` after the last day, and the loop stops.
public function advance(): bool
{
$this->at = $this->at + 1;
return $this->at < Core\Arr::count($this->days);
}
public function key(): string
{
return $this->days[$this->at];
}
public function current(): int
{
return $this->hours[$this->at];
}
}
class Timesheet implements KeyedIterable<string, int>
{
private array<string> $days;
private array<int> $hours;
public function constructor()
{
$this->days = [];
$this->hours = [];
}
public function log(string $day, int $hours): void
{
$this->days[] = $day;
$this->hours[] = $hours;
}
// `iterateKeyed` is the one method `KeyedIterable` needs. Each call returns a new cursor at Monday.
public function iterateKeyed(): KeyedIterator<string, int>
{
return new TimesheetCursor($this->days, $this->hours);
}
}
var $week = new Timesheet();
$week->log('Monday', 8);
$week->log('Tuesday', 7);
$week->log('Wednesday', 9);
$week->log('Thursday', 6);
// First walk: one line for each day.
foreach ($week as string $day => int $hours) {
echo $day, ': ', $hours, " h\n";
}
// Second walk over the same object. The loop calls `iterateKeyed` again, so it starts at Monday.
int $total = 0;
string $longest = '';
int $most = 0;
foreach ($week as string $day => int $hours) {
$total = $total + $hours;
if ($hours > $most) {
$most = $hours;
$longest = $day;
}
}
echo 'total: ', $total, " h\n";
echo 'longest day: ', $longest, "\n";
Output
Monday: 8 h
Tuesday: 7 h
Wednesday: 9 h
Thursday: 6 h
total: 30 h
longest day: Wednesday

Changelog

VersionChange
0.0.5foreach gives the key and the value of a Core\ObjectMap: write foreach ($map as var $key => var $value). A class of your own can do the same by implementing KeyedIterable<K, V>.