How to Delete The Last Element from an Indexed Array in PHP?

This tutorial explains how to delete the last element from an indexed array in PHP.

In PHP, there is a predefined function array_pop(), which will help us to delete the last element from an array.

Now we will give an example for deleting the last element from an array $a.

<html>
<body>
<?php
      $a=array("shilu","tarak","mona","rajat");
      array_pop();
         for($i=0;$i<count($a);$i=$i+1)
            {
               echo $a[$i]."<br>";
}
?>
</body>
</html>

Output:

shilu
tarak
mona

Here the array $a is containing 4 elements. The last element is deleted using the function array_pop(). After deleting the last array element from the array, the present array elements are:

Here $a[0]="shilu"=first array element value.
$a[1]="tarak"=second array element value.
$a[2]="mona"=third array element value.

Finally, we have printed the array element values with different lines using for loop and echoed statement and <br> tag.

See also:

Leave a Comment