How do you truncate a PHP array in a most effective way?
Should I use array_splice?
You can use the native functions to remove array elements:
With this knowledge make your own function
function array_truncate(array $array, $left, $right) { $array = array_slice($array, $left, count($array) - $left); $array = array_slice($array, 0, count($array) - $right); return $array; }
Demo - http://codepad.viper-7.com/JVAs0a
Yes, unless you want to loop over the array and unset() the unwanted elements.
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