Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I have an array where I want to use array_slice to remove the first element of the array. Here's the command:

$myArray = array_slice($myArray, 1);

Now, array_slice has a 4th argument, when set to true, it preserves the array keys instead of resetting them. I do need this option set to true.

The 3rd argument is to specify the length of the resulting array. You are supposed to leave this argument out if you want the array to be sliced to the end of the array instead of specifying a length.

So I tried this:

$myArray = array_slice($myArray, 1, NULL, true);

And this results in an empty array. What am I doing wrong? Is there another way to "leave out" the length argument without setting it to NULL? Because setting it to NULL seems to empty my array completely.

Also, my workaround is to do this:

$myArray = array_slice($myArray, 1, count($myArray)-1, true);

but it doesn't seem like I should have to do that...

UPDATE

This appears to be a bug with PHP 5.1.6

share|improve this question

migrated from serverfault.com Dec 1 '10 at 5:16

This question came from our site for system and network administrators.

    
Any particular reason why this question has been down-voted? – Jake Wilson Dec 1 '10 at 2:42
2  
The fact that it is off-topic for serverfault seems extremely likely. – Zoredache Dec 1 '10 at 3:04
    
Oh geez... I really thought I posted this on SO.... to whoever has the power, please move this topic to SO – Jake Wilson Dec 1 '10 at 3:34

I just tested this code:

$myArray = array(
'test1' => 'test1',
'test2' => 'test2',
'test3' => 'test3',
'test4' => 'test4',
);

var_dump(array_slice($myArray, 1, null, true));

And it works fine for me by only showing test2, test3, and test4.

This was run on 5.2.13. Try upgrading PHP (I know I should too but this is a dev box).

share|improve this answer
    
I'm on PHP 5.1.6, which is standard CentOS issue. Perhaps this is a bug. I'll try upgrading. – Jake Wilson Dec 1 '10 at 3:35
    
@Jakobud: Within that there's minor backport fixes. Try latest CentOS version and if not then try a later PHP version. I have a writeup on a super easy upgrade to PHP 5.2 or 5.3 with CentOS, just skip the nginx parts: rob.olmos.name/2010/08/centos-5-5-php-5-3-3-php-fpm-nginx-rpms – Rob Olmos Dec 1 '10 at 3:38

try this

$myArray = array_slice($myArray, 1, -1, true);
share|improve this answer
    
Nope, that removes the first element but slices all the way until the 2nd to the last element (the -1st element) in the array. So in a sense, it slices but the first and last element of the array off. – Jake Wilson Dec 1 '10 at 2:34

Couldn't you also use array_shift to remove an array's 1st element?

EDIT: It seems you want to preserve numerical keys, never mind then.

share|improve this answer

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.