Example 1:
$arr = array("a"=>"a", "5"=>"b", "c", "key"=>"d", "lastkey"=>"e"); print_r(end($arr));
Conclusion = e
Example 2:
ARRAY without keys.
$arr = array("a", "b", "c", "d", "e"); print_r(array_slice($arr, -1, 1, true));
Example 3:
ARRAY with key (s)
$arr = array("a"=>"a", "5"=>"b", "c", "key"=>"d", "lastkey"=>"e"); print_r(array_slice($arr, -1, 1, true));
Example 4:
If your keys are an array such as: [0] [1] [2] [3] [4] ... etc. You can use this:
$arr = array("a","b","c","d","e"); $lastindex = count($arr)-1; print_r($lastindex);
Output = 4
Example 5: But if you are not sure!
$arr = array("a"=>"a", "5"=>"b", "c", "key"=>"d", "lastkey"=>"e"); $ar_k = array_keys($arr); $lastindex = $ar_k [ count($ar_k) - 1 ]; print_r($lastindex);
Output = lastkey
Resources:
- https://php.net/array_slice
- https://www.php.net/manual/en/function.array-keys.php
- https://www.php.net/manual/en/function.count.php
- https://www.php.net/manual/en/function.end.php
Ali Feb 26 '11 at 23:30 2011-02-26 23:30
source share