Core\Arr::replaceRange
Core\Arr::replaceRange(
array<T> $a,
int $offset,
?int $length,
array<T> $replacement = [],
): array<T>Gives you a copy of an array with one window of entries replaced by other values. The array you pass in is never changed.
The window is the one Core\Arr::slice returns, so the positions are read the same way. $offset is where the window opens, and a negative offset counts back from the last entry. $length is how many entries the window holds, and a null length reaches the end of the array.
A length of 0 puts the new values in front of that position and removes nothing. An offset at the end of the array adds the values behind the last entry. Leave $replacement out and the window is removed.
The result is a list numbered from 0. The keys of the original are gone, and so are the keys of the replacement.
Parameters
$aarray<T>The array to edit.$offsetintThe position where the range starts. A negative offset works as inslice. An offset at the end appends.$length?intHow many entries the range has. A negative length works as inslice.0inserts at the position, andnullreaches the end.$replacementarray<T>default []The values to put in place of the range. Without it, the range is removed.
Return value
Returns array<T>. A list renumbered from "0", kept entries and replacement alike.
Examples
One window replaced
<?nvs// Shows how to replace one window of entries with other values.
array<string> $days = ['Mon', 'Tue', 'Wed', 'Thu'];
// The window opens at position 1 and holds 1 entry, and two values take its// place. This prints ["Mon","Sat","Sun","Wed","Thu"].echo Core\Json::encode(Core\Arr::replaceRange($days, 1, 1, ['Sat', 'Sun'])), "\n";
// Without a replacement the window is removed. This prints ["Mon","Thu"].echo Core\Json::encode(Core\Arr::replaceRange($days, 1, 2)), "\n";
// The original array is not changed. This prints ["Mon","Tue","Wed","Thu"].echo Core\Json::encode($days), "\n";["Mon","Sat","Sun","Wed","Thu"]["Mon","Thu"]["Mon","Tue","Wed","Thu"]Adding without removing
<?nvs// Shows how to add values without removing any. A window that holds no entries// is an insertion.
array<string> $queue = ['Ada', 'Grace'];
// A length of 0 puts the value in front of position 1 and removes nothing.// This prints ["Ada","Alan","Grace"].echo Core\Json::encode(Core\Arr::replaceRange($queue, 1, 0, ['Alan'])), "\n";
// An offset at the end of the array adds the value behind the last entry. This// prints ["Ada","Grace","Ida"].echo Core\Json::encode(Core\Arr::replaceRange($queue, 2, 0, ['Ida'])), "\n";
// A negative offset counts back from the last entry. This prints// ["Ada","Ina","Grace"].echo Core\Json::encode(Core\Arr::replaceRange($queue, -1, 0, ['Ina'])), "\n";["Ada","Alan","Grace"]["Ada","Grace","Ida"]["Ada","Ina","Grace"]Substitutes for a basket
<?nvs// Replaces one item of a basket with the two items a shop sends instead. The// substitutes stand where the missing item was, so the basket keeps its order.
array<string> $basket = ['tea', 'milk', 'bread'];int $missing = 1;array<string> $instead = ['oat drink', 'soy drink'];
// This prints `tea, oat drink, soy drink, bread`.array<string> $updated = Core\Arr::replaceRange($basket, $missing, 1, $instead);echo Core\Str::join($updated, ', '), "\n";
// The shop has no bread either, and nothing replaces it. This prints// `tea, oat drink, soy drink`.array<string> $sent = Core\Arr::replaceRange($updated, 3, 1);echo Core\Str::join($sent, ', '), "\n";tea, oat drink, soy drink, breadtea, oat drink, soy drink