|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2012-05-05 02:44 UTC] reeze dot xia at gmail dot com
[2012-05-05 02:56 UTC] reeze dot xia at gmail dot com
[2012-05-05 03:22 UTC] phristen at yahoo dot com
[2018-08-12 22:57 UTC] carusogabriel@php.net
[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
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 09:00:01 2025 UTC |
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