3v4l.org

run code in 300+ PHP versions simultaneously
<?php $input = array( 'a' => 'b', 'c' => 'd', 'e' => 'f', 'g' => 'h', 'i' => 'j', 'k' => 'l', ); $replacement = null; $offset = -3; // illegal offset - MAGIC? $length = 2; if ($replacement === null) { // + operator with null causes FATAL ERROR! $replacement = array(); } if (empty($input)) { $input = $replacement; return; } // split array at $offset and prevent keys (4th argument) $beforeItems = array_slice($input, 0, $offset, true); $afterItems = array_slice($input, $offset, null, true); if ($length !== null) { $length = (int)$length; // remove items while ($length > 0) { array_shift($afterItems); $length--; } } $input = $beforeItems + $replacement + $afterItems; print_r($input);

preferences:
36.8 ms | 402 KiB | 5 Q