|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #61946 Implement array_first() and array_last()
Submitted: 2012-05-05 02:22 UTC Modified: -
Avg. Score:5.0 ± 0.0
Reproduced:3 of 3 (100.0%)
Same Version:1 (33.3%)
Same OS:1 (33.3%)
From: phristen at yahoo dot com Assigned:
Status: Open Package: Arrays related
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: phristen at yahoo dot com
New email:
PHP Version: OS:


 [2012-05-05 02:22 UTC] phristen at yahoo dot com
Retrieving the first or the last element of an array (without modifying the array) is a very common task.

It is really annoying that PHP wouldn't come with built-in functions to do that.

Can you please implement the following 2 array functions:
array_first(array $array) - returns the first element of $array
array_last(array $array) - returns the last element of $array

Also consider implementing another pair of function to retrieve the keys: array_first_key and array_last_key.

Test script:
$test = array(
  100 => "a",
  200 => "b",
  300 => "c",
  400 => "d");

echo array_first($test)."\n";
echo array_last($test)."\n";
echo key($test)."\n";
echo implode(",", $test);

Expected result:

Actual result:
Fatal error:  Call to undefined function array_first() on line 3


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2012-05-05 02:44 UTC] reeze dot xia at gmail dot com
Hi, phristen,
  you request can be easily satisfied by using:

$array = array(1, 2, 3);

reset($array); // point to the first element
echo key($array); // the first key: 0
echo current($array); // first value

end($array); // now point to the last values
echo key($array); // last key
echo current($array); // last value

please refer to :

BTW: if you want to get all of keys of an array,
you could use array_keys()  but not key() :)

 [2012-05-05 02:56 UTC] reeze dot xia at gmail dot com
But from my point view, I'd like those functions. It really makes
code more easy to read.
 [2012-05-05 03:22 UTC] phristen at yahoo dot com
This is a very basic operation, and it should be done in a single function call.
I know about reset(), and I think it's ridiculous to have to call it every time I need to get the last element. It's also easy to 'forget' to reset it, and mess up your arrays.
And it's gonna turn into a terrible mess when you have to use first and last in a single statement (e.g. comparing first to last inside an if())

I just used key() in my example to demonstrate that array_last should not change the internal pointer.
 [2018-08-12 22:57 UTC]
> Also consider implementing another pair of function to retrieve the keys: array_first_key and array_last_key.

These two functions were implemented via in PHP 7.3
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Mon Apr 12 11:01:23 2021 UTC