46

I have an array that is structured like this:

[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 the array, but maintaining the index [35]?

The outcome that I am looking for is this:

[35] => Array
    (
        [time] => 1285571682
        [user] => test2
    )
Bhargav Rao
  • 45,811
  • 27
  • 120
  • 136
greenbandit
  • 2,247
  • 4
  • 29
  • 43

9 Answers9

152

try to use

end($array);
Alex Pliutau
  • 20,640
  • 26
  • 107
  • 142
  • 13
    This is the best way to get the _value_ of the last array element, but the OP asks for an array containing only the last element (with its proper index). However, +1 as this is what _most people_ googling this will need. – dotancohen Dec 08 '13 at 08:31
  • The OP explicitly asked ***... but maintaining the index [35]?***. The OP repeats this wish in the comment on Gumbo's answer. I think therefore it is misleading that this answer is selected as the accepted answer. The answer of @salathe best describes the behavior the OP asked for. – Paul van Leeuwen Dec 31 '16 at 07:44
68
$last = array_slice($array, -1, 1, true);

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

P.S. Unlike the other answers, this one actually does what you want. :-)

salathe
  • 50,055
  • 11
  • 103
  • 130
  • But this return other array not just the string – ssamuel68 May 28 '13 at 15:59
  • 5
    +1 This actually does what the question asked - it preserves the index and returns an array with a single element (which is what was implied by the example in the question). – JMTyler Jun 02 '13 at 03:17
10

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

$last = end($arr);
$last = current(array_slice($arr, -1));
Gumbo
  • 620,600
  • 104
  • 758
  • 828
  • but i need to preserve the index, like:[35] => Array ( [time] => 1285571682 [user] => test2 ) – greenbandit Sep 27 '10 at 07:45
  • If you need to preserve the index, you could use array_slice(). See http://stackoverflow.com/questions/3801891/get-last-value-in-array/3802007#3802007 – Martijn Heemels Sep 27 '10 at 08:16
4

If you have an array

$last_element = array_pop(array);
Vic
  • 20,681
  • 11
  • 77
  • 94
Govinda Yadav
  • 529
  • 4
  • 14
3

Example 1:

$arr = array("a"=>"a", "5"=>"b", "c", "key"=>"d", "lastkey"=>"e");
print_r(end($arr));

Output = e


Example 2:

ARRAY without key(s)

$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 array keys like : [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
Ali Han
  • 439
  • 5
  • 10
2

Like said Gumbo,

<?php

$fruits = array('apple', 'banana', 'cranberry');
echo end($fruits); // cranberry

?>
ipalaus
  • 2,233
  • 4
  • 26
  • 42
0

Another solution cold be:

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

The above will count the amount of array values, substract 1 and then return the value.

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

Peter
  • 8,127
  • 6
  • 53
  • 92
Yas
  • 4,117
  • 1
  • 34
  • 22
0

As the key is needed, the accepted solution doesn't work.

This:

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

will return exactly as the example in the question.

Vic
  • 20,681
  • 11
  • 77
  • 94
jmary
  • 218
  • 3
  • 11
0

"SPL-way":

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

Read more about SplFixedArray and why it's in some cases ( especially with big-index sizes array-data) more preferable than basic array here => The SplFixedArray class.

voodoo417
  • 11,580
  • 3
  • 31
  • 39