I know you can use the "array_pop" to remove the last element in the array. But if I wanted to remove the last 2 or 3 what would I do?
So how would I remove the last 2 elements in this array?
<?php
$stack = array("orange", "banana", "apple", "raspberry");
$fruit = array_pop($stack);
print_r($stack);
?>
                Use array_splice and specify the number of elements which you want to remove.
$stack = array("orange", "banana", "apple", "raspberry");
$fruit = array_splice($stack, -2);
print_r($stack);
Output
Array
(
    [0] => orange
    [1] => banana
)
                        You can use array_slice() with a negative length:
function array_remove($array, $n) {
    return array_slice($array, 0, -$n);
}
Test:
print_r( array_remove($stack, 2) );
Output:
Array
(
    [0] => orange
    [1] => banana
)
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With