PHP - 按索引范围获取数组记录

use*_*341 14 php arrays

嗨,您好,

是否有任何PHP本机函数根据索引的开头和结尾返回数组中的记录范围?

即:

array(0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd');
Run Code Online (Sandbox Code Playgroud)

现在我想只返回索引1和3之间的记录(b,c,d).

任何的想法?

pol*_*lau 23

难道你不能用例如array_slice吗?

$a = array(0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd');
array_slice($a, 1, 3); 
Run Code Online (Sandbox Code Playgroud)


ben*_*siu 12

array_slice有一个任务

array array_slice ( array $array , int $offset [, int $length [, bool $preserve_keys = false ]] )

例:

$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));