NogDog $test = [[1, 2], [3, 4], [5, 6]];
echo end($test)[1];
// outputs "6"
You can do the same thing with a function that returns a function.
function shorten_to(int $maxlength)
{
return fn(string $str) => substr($str, 0, $maxlength);
}
echo shorten_to(14)("Remove the cap after engaging the safety latch.");
And for that matter, an array element that contains a function: $ops['delete']($id)
.
YIL that the destructuring operator ...
can be used on Traversables, not just arrays (basically anything foreach
can use, i.e. for which is_iterable
returns true).
function shuffles(int $length, array $elements)
{
for($i = 0; $i < $length; ++$i) {
shuffle($elements);
yield $elements;
}
}
$flattened = array_merge(...shuffles(5, ['a', 'b', 'c', 'd']));
echo join($flattened);