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".end
Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.
end
Референца за `function.end.php` со подобрена типографија и навигација.
end
(PHP 4, PHP 5, PHP 7, PHP 8)
end — Поставете го внатрешниот покажувач на низата на последниот елемент
= NULL
end() advances arrayвнатрешниот покажувач на последниот елемент и ја враќа неговата вредност.
Параметри
array-
Низата. Оваа низа се предава по референца бидејќи се менува од функцијата. Ова значи дека мора да ѝ предадете вистинска променлива, а не функција што враќа низа бидејќи само вистински променливи може да се предадат по референца.
Вратени вредности
Враќа вредност на последниот елемент или false за празна низа.
Дневник на промени
| Верзија | = NULL |
|---|---|
| 8.1.0 | Повикувањето на оваа функција на objects е застарено. Или преобратете го object во array using get_mangled_object_vars() прво, или користете ги методите обезбедени од класа што имплементира Итератор, како на пр. ArrayIterator, наместо тоа. |
| 7.4.0 | Инстанци на SPL класите сега се третираат како празни објекти без својства наместо да се повикува Итератор метод со исто име како оваа функција. |
Примери
Пример #1 end() example
<?php
$fruits = array('apple', 'banana', 'cranberry');
echo end($fruits); // cranberry
?>Види Исто така
- current() - Врати ја тековната ставка во низата
- each() - Враќа пар од тековниот клуч и вредност од низа и го поместува курсорот на низата
- prev() - Премотајте го внатрешниот покажувач на низата
- reset() - Поставете го внатрешниот покажувач на низата на нејзиниот прв елемент
- next() - Помести го внатрешниот покажувач на низата
- array_key_last() - Ја добива последната клуч од низата
Белешки од корисници 5 белешки
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"
?>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.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)];
}
?>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.