0

I have the following array:

Array

Array (
[0] => Array (
  ...
)
[41] => Array (
  [name] => London 
  [company] => nhyt6t
  [top25_1] => 8.75912088
)
[42] => Array (
  [name] => Manchester
  [company] => gtr4rf
  [top25_1] => 6.56758398
)
  ...
[75] => Array (
  [name] => Leeds
  [company] => de3wsd6
  [top25_1] => 7.58675398
)
)

If my reading and understanding of http://www.php.net/manual/en/function.array-slice.php is correct, then the following should return just those within the array with an index of between 40 and 65.

$array = array_slice($array, 40, 65);

However, in practise, what this does is remove Indexes 0 through 39 but leaves everything else.

Can anyone explained where I am going wrong?

2 Answers 2

2

Just try with:

$array = array_slice($array, 40, 65 - 40);

So:

$array = array_slice($array, 40, 25);

We start slicing from 40 position and get 25 elements (ending on 40+25=65 position).

Sign up to request clarification or add additional context in comments.

1 Comment

65 - 40 + 1 if you want 65 to be included in the result.
1

Array slice chooses an offset and length, not a begin offset and end offset. It starts at the begin offset and chooses the next length elements. If your array has continuous indices (0,1,2,3,4...), then it will slice from [offset -> offset + length)

3 Comments

Must admit to struggling to get my head around the way this works.
My explanation, or the documentation?
Not your explanation - the documentation and concept! LOL, sorry if it came across differently!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.