如何在PHP中获取子数组?

wam*_*amp 42 php arrays

我想得到一个由4原始数组的第一行组成的数组$arr,

怎么用PHP做?

And*_*y E 17

你需要使用array_slice().

$output = array_slice($arr, 0, 4);
Run Code Online (Sandbox Code Playgroud)


小智 8

看看这个..这应该有帮助

<?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"

// note the differences in the array keys
print_r(array_slice($input, 2, -1));
print_r(array_slice($input, 2, -1, true));
?>
Run Code Online (Sandbox Code Playgroud)

来自http://docs.php.net/array_slice.