Is it possible to use array_shift() in PHP and get the key?

Posted by alex on Stack Overflow See other posts from Stack Overflow or by alex
Published on 2010-03-08T00:49:15Z Indexed on 2010/03/08 0:52 UTC
Read the original article Hit count: 301

Filed under:
|
|

I have a list of files in an array where the filename is the key and the value is the last modified date in seconds. They are sorted from oldest to newest.

The files are glob()'d in, and then sorted this way using

asort($fileNameToLastModified, SORT_NUMERIC);

I use array_shift() to get the oldest file. Unfortunately, it seems to be giving me the value, and there doesn't seem to be a way to get the key.

Would the only way to do that be something like this?

$keys = array_keys($fileNameToLastModified);

$oldest = array_shift($keys);
array_shift($fileNameToLastModified); // to manually chop the first array member off too.

Or is there a built in?

Thanks

© Stack Overflow or respective owner

Is it possible to use array_shift() in PHP and get the key?

Posted by alex on Stack Overflow See other posts from Stack Overflow or by alex
Published on 2010-03-08T00:49:15Z Indexed on 2010/03/08 3:51 UTC
Read the original article Hit count: 301

Filed under:
|
|

I have a list of files in an array where the filename is the key and the value is the last modified date in seconds. They are sorted from oldest to newest.

The files are glob()'d in, and then sorted this way using

asort($fileNameToLastModified, SORT_NUMERIC);

I use array_shift() to get the oldest file. Unfortunately, it seems to be giving me the value, and there doesn't seem to be a way to get the key.

Would the only way to do that be something like this?

$keys = array_keys($fileNameToLastModified);

$oldest = array_shift($keys);
array_shift($fileNameToLastModified); // to manually chop the first array member off too.

Or is there a built in?

Thanks

© Stack Overflow or respective owner

Related posts about php

Related posts about sorting