PHP array_splice()


array_splice() deletes or replaces the specified elements of an array after the specified offset. If only an offset is specified, all elements after offset will be deleted.

<?PHP
	$arr=array(1,2,3,4,5,6,7,8,9);
    $arr2=array_splice($arr,3);
	foreach($arr2 as $element) echo "$element, ";  //4, 5, 6, 7, 8, 9, 
	foreach($arr as $element) echo "$element, ";  //1, 2, 3, 
?>

If the purpose is not delete all elements after the offset, but only several elements after it, then the number of elements can be used as a parameter.
<?PHP
	$arr=array(1,2,3,4,5,6,7,8,9);
    $arr2=array_splice($arr,3,4);
	foreach($arr2 as $element) echo "$element, ";  //4, 5, 6, 7, 
	foreach($arr as $element) echo "$element, ";  //1, 2, 3, 8, 9 
?>

If the offset is negative, then all elements before the offset will be deleted.
<?PHP
	$arr=array(1,2,3,4,5,6,7,8,9);
    $arr2=array_splice($arr,-3);
	foreach($arr2 as $element) echo "$element, ";  //7, 8, 9, 
	foreach($arr as $element) echo "$element, ";  //1, 2, 3, 4, 5, 6, 
?>

Similarly the number of elements can be specified when the offset is negative.
<?PHP
	$arr=array(1,2,3,4,5,6,7,8,9);
    $arr2=array_splice($arr,-3, 2);
	foreach($arr2 as $element) echo "$element, ";  //7, 8, 
	foreach($arr as $element) echo "$element, ";  //1, 2, 3, 4, 5, 6, 9, 
?>