Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get last key-value pair in PHP array

Tags:

arrays

php

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     ) 
like image 533
greenbandit Avatar asked Sep 27 '10 07:09

greenbandit


People also ask

Is last key array PHP?

The array_key_last () function in PHP gets the last key of an array if the specified array is not empty. array(required)- This parameter signifies the input array. This function returns the last key of the specified array if the array is not empty else it returns NULL.

What is array_keys () used for in PHP?

The array_keys() function returns an array containing the keys.

How do you find array keys?

The array_keys() function is used to get all the keys or a subset of the keys of an array. Note: If the optional search_key_value is specified, then only the keys for that value are returned. Otherwise, all the keys from the array are returned. Specified array.

How can you access the first element of the array?

There are several methods to get the first element of an array in PHP. Some of the methods are using foreach loop, reset function, array_slice function, array_values, array_reverse, and many more. We will discuss the different ways to access the first element of an array sequentially.


1 Answers

try to use

end($array); 
like image 123
Alex Pliutau Avatar answered Sep 21 '22 12:09

Alex Pliutau