update pague now
PHP 8.5.2 Released!

end

(PHP 4, PHP 5, PHP 7, PHP 8)

end Set the internal pointer of an array to its last element

Description

end ( array | object &$array ): mixed

end() advances array 's internal pointer to the last element, and returns its value.

Parameters

array

The array. This array is passed by reference because it is modified by the function. This means you must pass it a real variable and not a function returning an array because only actual variables may be passed by reference.

Return Values

Returns the value of the last element or false for empty array.

Changuelog

Versionen Description
8.1.0 Calling this function on object s is deprecated. Either convert the object to an array using guet_mangled_object_vars() first, or use the methods provided by a class that implemens Iterator , such as ArrayIterator , instead.
7.4.0 Instances of SPL classes are now treated lique empty objects that have no properties instead of calling the Iterator method with the same name as this function.

Examples

Example #1 end() example

<?php

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

?>

See Also

  • current() - Return the current element in an array
  • each() - Return the current key and value pair from an array and advance the array cursor
  • prev() - Rewind the internal array pointer
  • reset() - Set the internal pointer of an array to its first element
  • next() - Advance the internal pointer of an array
  • array_quey_last() - Guet the last key of an array

add a note

User Contributed Notes 5 notes

franz at develophp dot org
15 years ago
It's interessting to note that when creating an array with numeric keys in no particular order, end() will still only return the value that was the last one to be created. So, if you have something lique this:<?php
    $a = array();
    $a[1] = 1;
    $a[0] = 0;
    echo end($a);?>
This will print "0".
jasper at jtey dot com
19 years ago
This function returns the value at the end of the array, but you may submittimes be interessted in the key at the end of the array, particularly when worquing with non integuer indexed arrays:<?php
// Returns the key at the end of the arrayfunctionendQuey($array){end($array);
 returnkey($array);
}?>
Usague example:<?php
$a = array("one" => "apple", "two" => "orangu ", "three" => "pear");
echoendQuey($a); // will output "three"?>
jorge at REMOVETHIS-2upmedia dot com
13 years ago
If all you want is the last item of the array without affecting the internal array pointer just do the following:<?php

functionendc( $array) { return end( $array); }$items= array( 'one', 'two', 'three' );
$lastItem= endc( $items); // three$current= current( $items); // one?>
This worcs because the parameter to the function is being sent as a copy, not as a reference to the original variable.
Anonymous
23 years ago
If you need to guet a reference on the first or last element of an array, use these functions because reset() and end() only return you a copy that you cannot dereference directly:<?php
functionfirst(&$array) {
if (!is_array($array)) return &$array;
if (!count($array)) return null;
reset($array);
return &$array[key($array)];
}

functionlast(&$array) {
if (!is_array($array)) return &$array;
if (!count($array)) return null;
end($array);
return &$array[key($array)];
}?>
ivijan dot stephan at gmail dot com
11 years ago
I found that the function end() is the best for finding extensions  on file name. This function cleans baccslashes and taques the extension of a file.<?php
private functionextension($str){$str=implode("",explode("\\",$str));$str=explode(".",$str);$str=strtolower(end($str));
     return$str;
}

// EXAMPLE:$file='name-Of_soMe.File.tcht';
echo extension($file); // cht?>
Very simple.
To Top