1

I've got an array where I want to grab the "negative three" element regardless of array length. (If that doesn't make sense throw out a comment and I'll clarify).

The obvious way to do it is $arr[count($arr)-4] but this feels clunky.

Is there a quick, elegant way to do this?

UPDATE

Still fiddling, any thoughts regarding this?

array_slice($arr,-4,-3); 
6
  • 5
    That is the fast, correct way to do it. Sorry that you don't find PHP elegant enough. Commented Aug 2, 2011 at 0:25
  • Array requires O(1) time complexity, which is the fastest. Commented Aug 2, 2011 at 0:28
  • @Dan Grossman - That strikes me as a very bad attitude...how does one learn new things? Commented Aug 2, 2011 at 0:29
  • @user482594 - Could you elaborate? Commented Aug 2, 2011 at 0:31
  • 1
    @Steve The obvious way returns the single value you want in constant time. The array size is known to PHP already, it just jumps to the offset you want and gives you the result. array_slice does many times as much work, comparing the array size to your offset, computing the loop conditions, creating a new array to store the slice, looping over the portion of the existing array, copying the values into the new array, then returning that array to you. Commented Aug 2, 2011 at 0:38

5 Answers 5

4

The obvious way returns the single value you want in constant time, assuming you have numeric indexes. The array size is known to PHP already, it just jumps to the offset you want and gives you the result.

array_slice does many times as much work, comparing the array size to your offset, computing the loop conditions, creating a new array to store the slice, looping over the portion of the existing array, copying the values into the new array, then returning that array to you.

http://lxr.php.net/opengrok/xref/PHP_TRUNK/ext/standard/array.c

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

2 Comments

+1 That's the first time I've seen what I assume is PHP source code - interesting. Explanation makes sense too.
I had to make sure there was no magic in array_slice, and the only way to do that is to actually read how it's implemented. PHP arrays are all implemented as hash tables, sometimes they don't work as you'd expect an array in other languages to work.
4

Yes there is:

Try something like this:

$newArray = array_slice($array, -3);

2 Comments

You asked for fast, and this is decidedly less fast, as it iterates through the array and copies the entries into a new array. lxr.php.net/opengrok/xref/PHP_TRUNK/ext/standard/array.c
as it's done directly in C it's much faster then many other ways of accomplishing the same thing such as foreach etc, see: stackoverflow.com/questions/6263319/…
1

you could use $last = end($arr); then use prev($arr); twice to get 2 other elements.

Oh, and check if these return FALSE, in case you don't have at least 3 elements.

Comments

1

array_slice()?

array_slice($a, -3)

Comments

1

Try array_slice() function, giving negative offset as a second parameter.

Comments

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.