Get the last key-value pair in a PHP array

I have an array that is structured as follows:

[33] => Array ( [time] => 1285571561 [user] => test0 ) [34] => Array ( [time] => 1285571659 [user] => test1 ) [35] => Array ( [time] => 1285571682 [user] => test2 ) 

How can I get the last value in an array, but maintaining the index [35]?

The result I'm looking for is this:

 [35] => Array ( [time] => 1285571682 [user] => test2 ) 
+46
arrays php
Sep 27 '10 at 7:38 on
source share
9 answers

try using

 end($array); 
+152
Sep 27 '10 at 7:41
source share
 $last = array_slice($array, -1, 1, true); 

See http://php.net/array_slice for more details on what arguments mean.

PS Unlike other answers, this one actually does what you want. :-)

+69
Sep 27 2018-10-10T00:
source share

You can use end to push the internal pointer to the end or array_slice to get an array containing only the last element:

 $last = end($arr); $last = current(array_slice($arr, -1)); 
+10
Sep 27 '10 at 7:40
source share

If you have an array

 $last_element = array_pop(array); 
+4
Sep 16 '14 at 4:48 on
source share

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)); // output is = array( [4] => e ) 



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)); // output is = array ( [lastkey] => e ) 



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:

  1. https://php.net/array_slice
  2. https://www.php.net/manual/en/function.array-keys.php
  3. https://www.php.net/manual/en/function.count.php
  4. https://www.php.net/manual/en/function.end.php
+3
Feb 26 '11 at 23:30
source share

As Gumbo said,

 <?php $fruits = array('apple', 'banana', 'cranberry'); echo end($fruits); // cranberry ?> 
+2
Sep 27 '10 at 7:41
source share

Another cool solution:

 $value = $arr[count($arr) - 1]; 

The above will count the number of array values, subtract 1 and then return the value.

Note. This can only be used if your array keys are numeric.

0
Dec 29 '15 at 14:17
source share

Since the key is needed, the decision does not work.

It:

 end($array); return array(key($array) => array_pop($array)); 

will come back exactly as an example in the question.

0
Jul 06 '16 at 3:39
source share

"SPL-way" :

 $splArray = SplFixedArray::fromArray($array); $last_item_with_preserved_index[$splArray->getSize()-1] = $splArray->offsetGet($splArray->getSize()-1); 

More about SplFixedArray and why in some cases (especially with massive data of large indices) it is preferable to the base array , here => Class SplFixedArray .

0
Mar 16 '19 at 1:22
source share



All Articles