16

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 method to do it?

1
  • for those who just want the key of first item (after sorting), array_keys($my_arr)[0] would work (though it'll be expensive for long arrays) Aug 12, 2021 at 3:26

6 Answers 6

24
$result = array_splice( $yourArray, 0, 1 );

... should do the trick. See array_splice.

2
  • I ended up just keeping what I had, because with this I still need to do an array_keys() and get an array member. But this is still another way to do it, so I'll accept it :)
    – alex
    Mar 8, 2010 at 1:34
  • 4
    I think you mean $result = key(array_splice( $yourArray, 0, 1 )); ?
    – cronoklee
    Jan 18, 2018 at 18:34
5

You could use each like:

$b = array(1=>2, 3=>4, 7=>3);
while(1) {
    list($key,$value) = each($b);
    if (empty($key))
        break;
    echo "$key $val\n";
}

Iterating the array with each will keep its last position.

3
+50

Different approach:

list ($key, $value) = each($srcData); array_shift($srcData);

... (or just list($key)... if you doesn't need $value). See each and list.

Edit: As @ztate pointed in his comment, each() function have been deprecated so relying in this approach is no longer a good idea.

But I think similar behaviour can be approached by using the new key() function this (untested) way:

$key = key($srcData); $selected = array_shift($srcData);
2
  • 3
    Warning: Each function has been DEPRECATED as of PHP 7.2.0. Relying on this function is highly discouraged.
    – zstate
    Jan 11, 2018 at 21:24
  • Whole PHP is deprecated for me. But good point: it seems it is a new victim of the usual PHP inconsistence across versions so this approach shoud not be used (even I don't trust it would save you from other changes). BTW I figure out similar behavior could be approached by new functions key(), current() and next(). Three function calls instead of one (despite the list() call that certainly could be avoided.
    – bitifet
    Jan 11, 2018 at 21:43
1

You could also do this:

<?php

$arr = array('a' => 'first', 'b' => 'second');

// This is your key,value shift line
foreach($arr as $k => $v) { break; }

echo "Key: $k\nValue: $k";

This will output:

Key: a
Value: first

I'm not sure how the performance is, so you might want to do some profiling, but it's likely to be faster than array_keys() for large arrays, since it doesn't need to iterate over the whole thing.

3
  • Since it copies the whole $arr, I'd say it could be terrible on big arrays.
    – zneak
    Mar 8, 2010 at 3:47
  • 2
    foreach() shouldn't copy the array, surely? Apr 23, 2010 at 2:27
  • 2
    It doesn't copy the array if all the body has is break.
    – alex
    Mar 19, 2013 at 3:47
1

You can take a look at key(), current() or each(). They do what you asked for.

I'm not really sure if you intend to actually get more key/value pairs from the array afterwards. So I won't get into any details of what else you might need to do.

1

Another possibility which is short working:

foreach ($array as $key => $value) break;
unset($array[$key]);

Your Answer

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

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