Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

HI there,

Is there any PHP native function which returns the range of records from the array based on the start and end of the index?

i.e.:

array(0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd');

and now I would like to only return records between index 1 and 3 (b, c, d).

Any idea?

share|improve this question
this question is synonymous with "PHP array_slice" us3.php.net/manual/en/function.array-slice.php – dreftymac Nov 2 '12 at 2:59
add comment (requires an account with 50 reputation)

3 Answers

Couldn't you do that with e.g. array_slice?

$a = array(0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd');
array_slice($a, 1, 3); 
share|improve this answer
add comment (requires an account with 50 reputation)

I think you're looking for array_slice.

share|improve this answer
add comment (requires an account with 50 reputation)

there is a task for array_slice

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

example:

$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));
share|improve this answer
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.