• 用 array_splice 可以裁剪,原来的数组会被改变,键值会重组
  • 用 array_slice ,原来的数组不会改变,键值可以不用重组

示例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
echo '<pre>';
$arrayRaw = array('2' => 'c','3' => 'd', '4' => 'e', '5' => 'f');
echo "0", PHP_EOL;
print_r(array_splice($arrayRaw, 2));
echo "1", PHP_EOL;
print_r($arrayRaw);

$arrayRaw = array('2' => 'c','3' => 'd', '4' => 'e', '5' => 'f');
array_slice($arrayRaw, 0, 2, true);
echo "2", PHP_EOL;
print_r(array_slice($arrayRaw, 0, 2, true));
echo "3", PHP_EOL;
print_r($arrayRaw);
echo '</pre>';

以上代码输出结果:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
0
Array
(
    [0] => e
    [1] => f
)
1
Array
(
    [0] => c
    [1] => d
)
2
Array
(
    [2] => c
    [3] => d
)
3
Array
(
    [2] => c
    [3] => d
    [4] => e
    [5] => f
)

** 参考链接 **

http://stackoverflow.com/questions/4804424/php-array-splice-keep-previous-keys