All TalkersCode Topics

Follow TalkersCode On Social Media

Get Last Element Of Array PHP

Last Updated : Jan 1, 2023

Get Last Element Of Array PHP

In this tutorial we will show you the solution of get last element of array PHP, as we know array will had more than two many values here we using end() and count() methods for getting last element from we defined array.

The end() function moves the internal pointer to and outputs the last element in the array.

The count() function returns the number of elements in an array but here we used to fetching the last index of element then we displayed.

Step By Step Guide On Get Last Element Of Array PHP :-

Here we defined an array namely ‘$names’ with some initialized values 'KTM','RAY-ZR','RE','JAWA'.

For user clarification we first printed our array elements by print_r() method with headlines then we using end() and count() methods.

In end() method we just passing our array into method then we gets results of last element from array $names.

For getting last element we used count() here count() will point overall count then it had result of one point more than last index value so we decreased by 1 then we displayed index of value.

<?php
    $names=['KTM','RAY-ZR','RE','JAWA'];
    echo "Displaying Last Element In Array<br>";
    echo "Our Array Values:<br>";
    print_r($names);
    echo "<br><br>We get the result using end() method ".end($names);
    echo "<br>We get the result using count() method ".$names[count($names)-1];
?>
  1. A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
  2. The default file extension for php files is “.php” and php statements end with ‘;’ semicolon.
  3. Here we defined array $names with some string values 'KTM','RAY-ZR','RE','JAWA'. Then we printed headline of program for describe about program concept.
  4. Then we printed our array $names elements and for instruct users before using end() or count() method how it will look.
  5. Then we applied end() method with parameter as array $names. So it will point the end position of element in array then we printed using echo() method.
  6. After we using count() method it will returns whole count of elements presents in array it had result of more than one value of last index value.
  7. So we decreasing by 1 value from whole count then we used as index of $names. Now it will definitely pointing last element then we displayed using echo() method.

Conclusion :-

In conclusion we are able to know how to get last element from array using php.

First we need to start our xampp server then we load this program on browser we can see headline about the program and before applying end() or count() method we displayed array with values then we displayed after applied both methods result of array so user can easily understand when seeing result on webpage.

You can define array with some other values like array with numeric values and associative array elements anything result will same but be aware when printing result.

I hope this tutorial on get last element of array PHP helps you and the steps and method mentioned above are easy to follow and implement.

Latest Tutorials