How to Get the last element of an array in PHP? The end() function is an inbuilt function in PHP Programming. The purpose of this function is to find the last element of the given input array. The end() function changes the internal pointer of an array to the last element and returns the value of the last element.

end() PHP Function available in PHP version 4, PHP version 5, PHP version 7

Return value

On Success:

The end() function returns the value of the last element of the array on success

On Failure:

FALSE on failure i.e, when the array is empty.

Syntax of end() PHP function

end ( array $array )

Sample code with end() PHP function

<?php

$source_array = ['key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3'];

$result = end($source_array);

echo "Last element: ".$result;

?>
Get the last element of an array in PHP – end()

Output / Result of end() PHP function

Last element: value3

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Sign In

Register

Reset Password

Please enter your username or email address, you will receive a link to create a new password via email.