PHP如何截断数组 [英] PHP how to truncate an array

查看:94
本文介绍了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 - 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆