有时候对于数组中的长度不满意,但是我们的数组已经建立好了,这时候在修改不太方便。如果要对长度进行限制的话, 换一种思路,用切片的想法对数组进行提取同样可以达到目的。
1、说明
array_slice()返回数组中指定下标offset和长度标记的子数组切片。
2、语法array_slice(array,start,length,preserve)
3、参数
array
start
length
Preserve
4、实例<?php
$input = array("a", "b", "c", "d", "e");
$output = array_slice($input, 2); // returns "c", "d", and "e"
$output = array_slice($input, -2, 1); // returns "d"
$output = array_slice($input, 0, 3); // returns "a", "b", and "c"
print_r(array_slice($input, 2, -1)); // array(0 => 'c', 1 => 'd');
print_r(array_slice($input, 2, -1, true)); // array(2 => 'c', 1 => 'd');
以上就是限制php数组长度的方法,大家在正式使用之前,可以对array_slice()函数的用法先进性了解,然后对长度进行限制的实战操作。更多php学习指路:
推荐操作系统:windows7系统、PHP5.6、DELL G3电脑