Hello friends,
Today, we will learn how to get the last element of an array using PHP. There are various ways of doing it which are as follows:
1. end(): PHP proivdes an in-built function which returns the last element of an array end().
Syntax:
end ( $array )
$array is the array whose last element needed to find out.
Example:
<?php
$d = array('a', 'b', 'c');
echo end($d);
?>
It will return c.
2. Next way is to count array elements and then decrement it by 1 as follows:
<?php
$d = array('a', 'b', 'c');
echo end($d);
echo $d[sizeof($d)-1];
?>
0 Comment(s)