- How to get the last element of an array without deleting it?
- 33 Answers 33
- For PHP >= 7.3.0:
- PHP Array Pop: The Ultimate Guide
- What is PHP Array Pop?
- Syntax of PHP Array Pop
- Examples of PHP Array Pop
- Example 1: Removing the Last Element from an Array
- Example 2: Using PHP Array Pop on an Empty Array
- Conclusion
- PHP: array_pop() function
- PHP: Tips of the Day
How to get the last element of an array without deleting it?
Ok, I know all about array_pop() , but that deletes the last element. How to get the last element of an array without deleting it? Here’s a bonus:
$array = array('a' => 'a', 'b' => 'b', 'c' => 'c');
$array = array('a', 'b', 'c', 'd'); unset($array[2]); echo $array[sizeof($array) - 1]; // Output: PHP Notice: Undefined offset: 2 in - on line 4
Believe it or not popping it and putting it back on is one of the fastest ways I benchmarked doing this. $val=$array[]=array_pop($array); echo $val;
This question resulted in many options. To help myself choose, I did some comparison of most notable / distinct options and shared the results as a separate answer. (:@user2782001 suggested my favorite so far in the comment above.:) Thanks to all for contributing!
@TheodoreRSmith When PHP 7.3 is released you could consider making (this suggestion by Quasimodo’s clone the ‘accepted answer’ (for your consideration).
33 Answers 33
$myLastElement = end($yourArray);
Note that this doesn’t just return the last element of the passed array, it also modifies the array’s internal pointer, which is used by current , each , prev , and next .
For PHP >= 7.3.0:
If you are using PHP version 7.3.0 or later, you can use array_key_last , which returns the last key of the array without modifying its internal pointer. So to get the last value, you can do:
$myLastElement = $yourArray[array_key_last($yourArray)];
@DavidMurdoch Perhaps, but it sure does churn the RAM and CPU, creating the temp array for the array values.
If your server is consuming too much RAM so that calling one simple extra function is a deal breaker, I suggest you re-examine your server’s configuration and resources.
The many answers in this thread present us with many different options. To be able to choose from them I needed to understand their behavior and performance. In this answer I will share my findings with you, benchmarked against PHP versions 5.6.38 , 7.2.10 and 7.3.0RC1 (expected Dec 13 2018).
The options ( > s) I will test are:
- option .1. $x = array_values(array_slice($array, -1))[0]; (as suggested by rolacja)
- option .2. $x = array_slice($array, -1)[0]; (as suggested by Stoutie)
- option .3. $x = array_pop((array_slice($array, -1))); (as suggested by rolacja)
- option .4. $x = array_pop((array_slice($array, -1, 1))); (as suggested by Westy92)
- option .5. $x = end($array); reset($array); (as suggested by Iznogood)
- option .6. $x = end((array_values($array))); (as suggested by TecBrat)
- option .7. $x = $array[count($array)-1]; (as suggested by Mirko Pagliai)
- option .8. $keys = array_keys($array); $x = $array[$keys[count($keys)-1]]; (as suggested by thrau)
- option .9. $x = $array[] = array_pop($array); (as suggested by user2782001)
- option 10. $x = $array[array_key_last($array)]; (as suggested by Quasimodo’s clone ; available per PHP 7.3)
The test inputs ( > s) to combine with:
- null = $array = null;
- empty = $array = [];
- last_null = $array = [«a»,»b»,»c»,null];
- auto_idx = $array = [«a»,»b»,»c»,»d»];
- shuffle = $array = []; $array[1] = «a»; $array[2] = «b»; $array[0] = «c»;
- 100 = $array = []; for($i=0;$i
- 100000 = $array = []; for($i=0;$i
For testing I will use the 5.6.38 , 7.2.10 and 7.3.0RC1 PHP docker containers like:
sudo docker run -it --rm php:5.6.38-cli-stretch php -r '>>'
Each combination of the above listed > s and > s will be run on all versions of PHP. For each test run the following code snippet is used:
> error_reporting(E_ALL); > error_reporting(0); $before=microtime(TRUE); for($i=0;$i<100;$i++) >>; $after=microtime(TRUE); echo "\n"; var_dump($x); echo round(($after-$before)/(100*100)*1000*1000*1000);
For each run this will var_dump the last retrieved last value of the test input and print the average duration of one iteration in femtoseconds (0.000000000000001th of a second).
The results are as follows:
/==========================================================================================================================================================================================================================================================================================================================================================================================================================\ || || T E S T I N P U T - 5 . 6 . 3 8 || T E S T I N P U T - 7 . 2 . 1 0 || T E S T I N P U T - 7 . 3 . 0 R C 1 || || || null | empty | last_null | auto_idx | shuffle | 100 | 100000 || null | empty | last_null | auto_idx | shuffle | 100 | 100000 || null | empty | last_null | auto_idx | shuffle | 100 | 100000 || ||============================OPTIONS - ERRORS==========================++===============+===============+===============+===============+===============+===============+===============++===============+===============+===============+===============+===============+===============+===============++===============+===============+===============+===============+===============+===============+===============<| || 1. $x = array_values(array_slice($array, -1))[0]; || W1 + W2 | N1 | - | - | - | - | - || W1 + W2 | N1 | - | - | - | - | - || W1 + W2 | N1 | - | - | - | - | - || || 2. $x = array_slice($array, -1)[0]; || W1 | N1 | - | - | - | - | - || W1 | N1 | - | - | - | - | - || W1 | N1 | - | - | - | - | - || || 3. $x = array_pop((array_slice($array, -1))); || W1 + W3 | - | - | - | - | - | - || W1 + N2 + W3 | N2 | N2 | N2 | N2 | N2 | N2 || W1 + N2 + W3 | N2 | N2 | N2 | N2 | N2 | N2 || || 4. $x = array_pop((array_slice($array, -1, 1))); || W1 + W3 | - | - | - | - | - | - || W1 + N2 + W3 | N2 | N2 | N2 | N2 | N2 | N2 || W1 + N2 + W3 | N2 | N2 | N2 | N2 | N2 | N2 || || 5. $x = end($array); reset($array); || W4 + W5 | - | - | - | - | - | - || W4 + W5 | N2 | N2 | N2 | N2 | N2 | N2 || W4 + W5 | - | - | - | - | - | - || || 6. $x = end((array_values($array))); || W2 + W4 | - | - | - | - | - | - || W2 + N2 + W4 | - | - | - | - | - | - || W2 + N2 + W4 | N2 | N2 | N2 | N2 | N2 | N2 || || 7. $x = $array[count($array)-1]; || - | N3 | - | - | - | - | - || W7 | N3 | - | - | - | - | - || W7 | N3 | - | - | - | - | - || || 8. $keys = array_keys($array); $x = $array[$keys[count($keys)-1]]; || W6 | N3 + N4 | - | - | - | - | - || W6 + W7 | N3 + N4 | - | - | - | - | - || W6 + W7 | N3 + N4 | - | - | - | - | - || || 9. $x = $array[] = array_pop($array); || W3 | - | - | - | - | - | - || W3 | - | - | - | - | - | - || W3 | - | - | - | - | - | - || || 10. $x = $array[array_key_last($array)]; || F1 | F1 | F1 | F1 | F1 | F1 | F1 || F2 | F2 | F2 | F2 | F2 | F2 | F2 || W8 | N4 | F2 | F2 | F2 | F2 | F2 || ||========================OPTIONS - VALUE RETRIEVED=====================++===============+===============+===============+===============+===============+===============+===============++===============+===============+===============+===============+===============+===============+===============++===============+===============+===============+===============+===============+===============+===============<| || 1. $x = array_values(array_slice($array, -1))[0]; || NULL | NULL | NULL | string(1) "d" | string(1) "c" | int(99) | int(99999) || NULL | NULL | NULL | string(1) "d" | string(1) "c" | int(99) | int(99999) || NULL | NULL | NULL | string(1) "d" | string(1) "c" | int(99) | int(99999) || || 2. $x = array_slice($array, -1)[0]; || NULL | NULL | NULL | string(1) "d" | string(1) "c" | int(99) | int(99999) || NULL | NULL | NULL | string(1) "d" | string(1) "c" | int(99) | int(99999) || NULL | NULL | NULL | string(1) "d" | string(1) "c" | int(99) | int(99999) || || 3. $x = array_pop((array_slice($array, -1))); || NULL | NULL | NULL | string(1) "d" | string(1) "c" | int(99) | int(99999) || NULL | NULL | NULL | string(1) "d" | string(1) "c" | int(99) | int(99999) || NULL | NULL | NULL | string(1) "d" | string(1) "c" | int(99) | int(99999) || || 4. $x = array_pop((array_slice($array, -1, 1))); || NULL | NULL | NULL | string(1) "d" | string(1) "c" | int(99) | int(99999) || NULL | NULL | NULL | string(1) "d" | string(1) "c" | int(99) | int(99999) || NULL | NULL | NULL | string(1) "d" | string(1) "c" | int(99) | int(99999) || || 5. $x = end($array); reset($array); || NULL | bool(false) | NULL | string(1) "d" | string(1) "c" | int(99) | int(99999) || NULL | bool(false) | NULL | string(1) "d" | string(1) "c" | int(99) | int(99999) || NULL | bool(false) | NULL | string(1) "d" | string(1) "c" | int(99) | int(99999) || || 6. $x = end((array_values($array))); || NULL | bool(false) | NULL | string(1) "d" | string(1) "c" | int(99) | int(99999) || NULL | bool(false) | NULL | string(1) "d" | string(1) "c" | int(99) | int(99999) || NULL | bool(false) | NULL | string(1) "d" | string(1) "c" | int(99) | int(99999) || || 7. $x = $array[count($array)-1]; || NULL | NULL | NULL | string(1) "d" | string(1) "b" | int(99) | int(99999) || NULL | NULL | NULL | string(1) "d" | string(1) "b" | int(99) | int(99999) || NULL | NULL | NULL | string(1) "d" | string(1) "b" | int(99) | int(99999) || || 8. $keys = array_keys($array); $x = $array[$keys[count($keys)-1]]; || NULL | NULL | NULL | string(1) "d" | string(1) "c" | int(99) | int(99999) || NULL | NULL | NULL | string(1) "d" | string(1) "c" | int(99) | int(99999) || NULL | NULL | NULL | string(1) "d" | string(1) "c" | int(99) | int(99999) || || 9. $x = $array[] = array_pop($array); || NULL | NULL | NULL | string(1) "d" | string(1) "c" | int(99) | int(99999) || NULL | NULL | NULL | string(1) "d" | string(1) "c" | int(99) | int(99999) || NULL | NULL | NULL | string(1) "d" | string(1) "c" | int(99) | int(99999) || || 10. $x = $array[array_key_last($array)]; || N/A | N/A | N/A | N/A | N/A | N/A | N/A || N/A | N/A | N/A | N/A | N/A | N/A | N/A || N/A | N/A | N/A | N/A | N/A | N/A | N/A || ||=================OPTIONS - FEMTOSECONDS PER ITERATION=================++===============+===============+===============+===============+===============+===============+===============++===============+===============+===============+===============+===============+===============+===============++===============+===============+===============+===============+===============+===============+===============<| || 1. $x = array_values(array_slice($array, -1))[0]; || 803 | 466 | 390 | 384 | 373 | 764 | 1.046.642 || 691 | 252 | 101 | 128 | 93 | 170 | 89.028 || 695 | 235 | 90 | 97 | 95 | 188 | 87.991 || || 2. $x = array_slice($array, -1)[0]; || 414 | 349 | 252 | 248 | 246 | 604 | 1.038.074 || 373 | 249 | 85 | 91 | 90 | 164 | 90.750 || 367 | 224 | 78 | 85 | 80 | 155 | 86.141 || || 3. $x = array_pop((array_slice($array, -1))); || 724 | 228 | 323 | 318 | 350 | 673 | 1.042.263 || 988 | 285 | 309 | 317 | 331 | 401 | 88.363 || 877 | 266 | 298 | 300 | 326 | 403 | 87.279 || || 4. $x = array_pop((array_slice($array, -1, 1))); || 734 | 266 | 358 | 356 | 349 | 699 | 1.050.101 || 887 | 288 | 316 | 322 | 314 | 408 | 88.402 || 935 | 268 | 335 | 315 | 313 | 403 | 86.445 || || 5. $x = end($array); reset($array); || 715 | 186 | 185 | 180 | 176 | 185 | 172 || 674 | 73 | 69 | 70 | 66 | 65 | 70 || 693 | 65 | 85 | 74 | 68 | 70 | 69 || || 6. $x = end((array_values($array))); || 877 | 205 | 320 | 337 | 304 | 2.901 | 7.921.860 || 948 | 300 | 336 | 308 | 309 | 509 | 29.696.951 || 946 | 262 | 301 | 309 | 302 | 499 | 29.234.928 || || 7. $x = $array[count($array)-1]; || 123 | 300 | 137 | 139 | 143 | 140 | 144 || 312 | 218 | 48 | 53 | 45 | 47 | 51 || 296 | 217 | 46 | 44 | 53 | 53 | 55 || || 8. $keys = array_keys($array); $x = $array[$keys[count($keys)-1]]; || 494 | 593 | 418 | 435 | 399 | 3.873 | 12.199.450 || 665 | 407 | 103 | 109 | 114 | 431 | 30.053.730 || 647 | 445 | 91 | 95 | 96 | 419 | 30.718.586 || || 9. $x = $array[] = array_pop($array); || 186 | 178 | 175 | 188 | 180 | 181 | 186 || 83 | 78 | 75 | 71 | 74 | 69 | 83 || 71 | 64 | 70 | 64 | 68 | 69 | 81 || || 10. $x = $array[array_key_last($array)]; || N/A | N/A | N/A | N/A | N/A | N/A | N/A || N/A | N/A | N/A | N/A | N/A | N/A | N/A || 370 | 223 | 49 | 52 | 61 | 57 | 52 || \=========================================================================================================================================================================================================================================================================================================================================================================================================================/
The above mentioned Fatal, Warning and Notice codes translate as:
F1 = Fatal error: Call to undefined function array_key_last() in Command line code on line 1 F2 = Fatal error: Uncaught Error: Call to undefined function array_key_last() in Command line code:1 W1 = Warning: array_slice() expects parameter 1 to be array, null given in Command line code on line 1 W2 = Warning: array_values() expects parameter 1 to be array, null given in Command line code on line 1 W3 = Warning: array_pop() expects parameter 1 to be array, null given in Command line code on line 1 W4 = Warning: end() expects parameter 1 to be array, null given in Command line code on line 1 W5 = Warning: reset() expects parameter 1 to be array, null given in Command line code on line 1 W6 = Warning: array_keys() expects parameter 1 to be array, null given in Command line code on line 1 W7 = Warning: count(): Parameter must be an array or an object that implements Countable in Command line code on line 1 W8 = Warning: array_key_last() expects parameter 1 to be array, null given in Command line code on line 1 N1 = Notice: Undefined offset: 0 in Command line code on line 1 N2 = Notice: Only variables should be passed by reference in Command line code on line 1 N3 = Notice: Undefined offset: -1 in Command line code on line 1 N4 = Notice: Undefined index: in Command line code on line 1
Based on this output I draw the following conclusions:
- newer versions of PHP perform better with the exception of these options that became significantly slower:
- option .6. $x = end((array_values($array)));
- option .8. $keys = array_keys($array); $x = $array[$keys[count($keys)-1]];
- option .5. $x = end($array); reset($array);
- option .7. $x = $array[count($array)-1];
- option .9. $x = $array[] = array_pop($array);
- option 10. $x = $array[array_key_last($array)]; (since PHP 7.3)
- option .7. $x = $array[count($array)-1]; (due to use of count )
- option .9. $x = $array[] = array_pop($array); (due to assigning value losing original key)
- option .5. $x = end($array); reset($array);
- option .6. $x = end((array_values($array)));
- option 10. $x = $array[array_key_last($array)]; (since PHP 7.3)
A bit depending on whether using the array as stack or as queue you can make variations on option 9.
PHP Array Pop: The Ultimate Guide
Are you looking for a comprehensive guide on PHP Array Pop? Look no further! In this article, we will provide a thorough explanation of this useful PHP function and demonstrate its usage through various examples.
What is PHP Array Pop?
PHP Array Pop is a built-in function in PHP that allows you to remove the last element from an array and return it. This function is useful in various scenarios where you need to manage arrays dynamically.
Syntax of PHP Array Pop
The syntax of PHP Array Pop is straightforward and easy to understand. The function takes an array as an argument and returns the last element of that array after removing it. The syntax is as follows:
array_pop (array &$array) : mixed
The argument $array is the array that you want to remove the last element from. The function returns the value of the removed element.
Examples of PHP Array Pop
Let's now take a look at some practical examples of how you can use PHP Array Pop.
Example 1: Removing the Last Element from an Array
$fruits = array("apple", "banana", "cherry"); $last_fruit = array_pop($fruits); print_r($fruits); echo $last_fruit; ?>
In this example, we have an array of fruits, $fruits , and we use PHP Array Pop to remove the last element, "cherry", from the array. The function returns "cherry" as the value of the removed element and updates the $fruits array to exclude the last element.
Array ( [0] => apple [1] => banana ) cherry
Example 2: Using PHP Array Pop on an Empty Array
$empty_array = array(); $last_element = array_pop($empty_array); if (empty($empty_array)) < echo "Array is empty"; > if (is_null($last_element)) < echo "No element to pop"; > ?>
In this example, we have an empty array $empty_array and we use PHP Array Pop to remove the last element. The function returns NULL as the value of the removed element, which indicates that there was no element to pop.
Array is empty No element to pop
Conclusion
In conclusion, PHP Array Pop is a simple and useful function for managing arrays in PHP. Whether you are removing the last element from an array or checking if an array is empty, this function can save you a lot of time and effort. So make sure to include it in your toolkit of PHP functions.
This article has provided a comprehensive guide to PHP Array Pop. If you follow the examples and guidelines outlined in this article, you should be able to use this function with confidence in your own projects.
We hope you found this article helpful. If you have any questions or comments, please don't hesitate to reach out to us.
PHP: array_pop() function
The array_pop() function is used to remove the last element of an array. For an empty array, the function returns NULL.
Note: This function will reset() the array pointer of the input array after use.
Name Description Required /
OptionalType array_pop The specified array whose last element will be removed. Required Array Return value:
The last value of array_pop.
Value Type: Mixed*.
*Mixed: Mixed indicates multiple (but not necessarily all) types.
Array ( [0] => Mathematics [1] => Physics [2] => Chemistry )
Pictorial Presentation:
Practice here online :
Previous: array_pad
Next: array_pushFollow us on Facebook and Twitter for latest update.
PHP: Tips of the Day
Best way to find this is: create a php file and add the following code:
and open it in browser, it will show the file which is actually being read!
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook