PHP如何截断数组 [英] PHP how to truncate an array
本文介绍了PHP如何截断数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何以最有效的方式截断PHP数组?
How do you truncate a PHP array in a most effective way?
我应该使用 array_splice 吗?
推荐答案
您可以使用本机函数删除数组元素:
You can use the native functions to remove array elements:
- array_pop -从数组末尾弹出元素
- array_shift -将元素移出数组的开头
- array_slice -提取数组的一部分
- 取消设置-从数组中删除一个元素
- array_pop - Pop the element off the end of array
- array_shift - Shift an element off the beginning of array
- array_slice - Extract a slice of the array
- unset - Remove one element from array
借助这些知识,您可以发挥自己的作用
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;
}
演示- http://codepad.viper-7.com/JVAs0a
Demo - http://codepad.viper-7.com/JVAs0a
这篇关于PHP如何截断数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文