end

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

endPosiciona el puntero del array al final del array

Descripción

end(array|object &$array): mixed

end() desplaza el puntero interno del array array hasta el último elemento y devuelve su valor.

Parámetros

array

El array. Este array es pasado por referencia ya que será modificado por la función. Esto significa que debe pasar una verdadera variable y no una función que devuelva un array, ya que actualmente, solo las variables pueden ser pasadas por referencia.

Valores devueltos

Devuelve el valor del último elemento o false si el array está vacío.

Historial de cambios

Versión Descripción
8.1.0 Calling this function on objects is deprecated. Either convert the object to an array using get_mangled_object_vars() first, or use the methods provided by a class that implements Iterator, such as ArrayIterator, instead.
7.4.0 Instances of SPL classes are now treated like empty objects that have no properties instead of calling the Iterator method with the same name as this function.

Ejemplos

Ejemplo #1 Ejemplo con end()

<?php

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

?>

Ver también

  • current() - Devuelve el elemento actual del array
  • each() - Devuelve cada par clave/valor de un array
  • prev() - Retrocede el puntero actual del array
  • reset() - Reinicia el puntero interno del array al principio
  • next() - Avance el puntero interno de un array
  • array_key_last() - Recupera la última clave de un array

add a note

User Contributed Notes 5 notes

up
135
franz at develophp dot org
14 years ago
It's interesting 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 like this:

<?php
$a
= array();
$a[1] = 1;
$a[0] = 0;
echo
end($a);
?>

This will print "0".
up
35
jasper at jtey dot com
18 years ago
This function returns the value at the end of the array, but you may sometimes be interested in the key at the end of the array, particularly when working with non integer indexed arrays:

<?php
// Returns the key at the end of the array
function endKey($array){
end($array);
return
key($array);
}
?>

Usage example:
<?php
$a
= array("one" => "apple", "two" => "orange", "three" => "pear");
echo
endKey($a); // will output "three"
?>
up
24
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

function endc( $array ) { return end( $array ); }

$items = array( 'one', 'two', 'three' );
$lastItem = endc( $items ); // three
$current = current( $items ); // one
?>

This works because the parameter to the function is being sent as a copy, not as a reference to the original variable.
up
31
Anonymous
22 years ago
If you need to get 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
function first(&$array) {
if (!
is_array($array)) return &$array;
if (!
count($array)) return null;
reset($array);
return &
$array[key($array)];
}

function
last(&$array) {
if (!
is_array($array)) return &$array;
if (!
count($array)) return null;
end($array);
return &
$array[key($array)];
}
?>
up
12
ivijan dot stefan at gmail dot com
10 years ago
I found that the function end() is the best for finding extensions on file name. This function cleans backslashes and takes the extension of a file.

<?php
private function extension($str){
$str=implode("",explode("\\",$str));
$str=explode(".",$str);
$str=strtolower(end($str));
return
$str;
}

// EXAMPLE:
$file='name-Of_soMe.File.txt';
echo
extension($file); // txt
?>

Very simple.
To Top