获取两个位置之间的数组值

时间:2016-02-06 00:52:48

标签: php

我有例如数组:

$list = array(40, 22, 60, 50);

$start = 40;
$end = 60;

我想获得$ start和$ end之间的值(可以是$ start和$ end)。对于这个例子,我想收到:

array(40, 22, 60);

如果:

$start = 22;
$end = 60;

然后我想收到:

array(22, 60);

AND IF:

$list = array(40, 22, 60, 50, 100, 200, 70);

$start = 22;
$end = 200;

我想收到:     数组(22,60,50,100,200);

我该怎么做?

1 个答案:

答案 0 :(得分:1)

你可以遍历数组,但还有另一种方法:

$list = array(40, 22, 60, 50, 100, 200, 70);
$start = 22;
$end = 200;

$start_position = array_search($start, $list);
$end_position = array_search($end, $list);
$values = array_slice($list, $start_position, $end_position - $start_position + 1);