php.net |  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: 2021-04-27 13:41 UTC
Votes:3
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: cmb (profile)
Status: Closed Package: Arrays related
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
23 - 6 = ?
Subscribe to this entry?

 
 [2012-05-05 02:22 UTC] phristen at yahoo dot com
Description:
------------
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:
----------------
a
d
100
a,b,c,d

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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

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 : http://www.php.net/manual/en/function.reset.php
----------------------

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

Thanks.
 [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())

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

These two functions were implemented via https://wiki.php.net/rfc/array_key_first_last in PHP 7.3
 [2021-04-27 13:41 UTC] cmb@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: cmb
 [2021-04-27 13:41 UTC] cmb@php.net
Indeed, array_key_first() and array_key_last() are available as of
PHP 7.3.0.  array_value_first() and array_value_last() have been
explicitly rejected, since the easily can be implemented in
userland (or used inline).
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 07:01:27 2024 UTC