end
(PHP 4, PHP 5)
end — Set the internal pointer of an array to its last element
Description
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.
Examples
Example #1 end() example
<?php
$fruits = array('apple', 'banana', 'cranberry');
echo end($fruits); // cranberry
?>
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 array pointer of an array
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Расширения, относящиеся к переменным и типам
- Массивы
- array_change_key_case
- array_chunk
- array_column
- array_combine
- array_count_values
- array_diff_assoc
- array_diff_key
- array_diff_uassoc
- array_diff_ukey
- array_diff
- array_fill_keys
- array_fill
- array_filter
- array_flip
- array_intersect_assoc
- array_intersect_key
- array_intersect_uassoc
- array_intersect_ukey
- array_intersect
- array_key_exists
- array_keys
- array_map
- array_merge_recursive
- array_merge
- array_multisort
- array_pad
- array_pop
- array_product
- array_push
- array_rand
- array_reduce
- array_replace_recursive
- array_replace
- array_reverse
- array_search
- array_shift
- array_slice
- array_splice
- array_sum
- array_udiff_assoc
- array_udiff_uassoc
- array_udiff
- array_uintersect_assoc
- array_uintersect_uassoc
- array_uintersect
- array_unique
- array_unshift
- array_values
- array_walk_recursive
- array_walk
- array
- arsort
- asort
- compact
- count
- current
- each
- end
- extract
- in_array
- key_exists
- key
- krsort
- ksort
- list
- natcasesort
- natsort
- next
- pos
- prev
- range
- reset
- rsort
- shuffle
- sizeof
- sort
- uasort
- uksort
- usort
Коментарии
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)];
}
?>
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"
?>
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".
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.
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.