All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Remove Last Element From Array PHP

Last Updated : Mar 11, 2024

Remove Last Element From Array PHP

In this article we will show you the solution of remove last element from array PHP, an array is a homogeneous collection of elements. By using php we can delete the last element from an array.

We will use two different methods to delete the last element :-

  • Array_pop(): this php function deletes the array’s last element.
  • Unset(): by using unset() function we can delete any specified element.

Step By Step Guide On Remove Last Element From Array PHP :-

Let’s see examples of two different methods below.

Method 1

In this method, we will remove the last element from the array by using the array_pop() method.

<!DOCTYPE html>
<html lang = " en " >
<head>
    <meta charset = " UTF - 8" >
    <meta http-equiv = " X-UA-Compatible " content = " IE=edge " >
    <meta name = " viewport " content = " width = device-width , initial-scale = 1.0 " >
    <title> remove last element from array php </title>
</head>
<body>
    <h1 style=" color : rgb(113, 221, 113) ;"> TALKERSCODE </h1>
    <h2> remove last element from array php </h2>
    <?php
$myArray = array ('orange', 'apple', 'grapes', 'olive', 'guava', 'lemon', 'avocado') ;
echo '<pre>' ;
echo ' array before : ' ;
print_r ($myArray) ;
array_pop ($myArray) ;
echo ' array after : ' ;
print_r ($myArray) ;
?>
</body>
</html>
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As above now the <head> tag is used to contain information about the web page. In this tag, a <title> tag is used which helps us to specify a webpage title.
  4. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  5. Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
  6. <h1> tag used to add heading here.
  7. Now we close the HTML file with </html> tag
  8. we write <?php tag to write PHP within it.
  9. First create an array with some values.
  10. We first print the array before deleting the last element.
  11. Using the array_pop() method to remove the last element.
  12. Using print_r() to display the final array

Method 2

In the example below we used the unset() function. we used this method only if we know the value of the last element.

<!DOCTYPE html>
<html lang = " en " >
<head>
    <meta charset = " UTF - 8" >
    <meta http-equiv = " X-UA-Compatible " content = " IE=edge " >
    <meta name = " viewport " content = " width = device-width , initial-scale = 1.0 " >
    <title> remove last element from array php </title>
</head>
<body>
    <h1 style=" color : rgb(113, 221, 113) ;"> TALKERSCODE </h1>
    <h2> remove last element from array php </h2>
    <?php
    $myArray = array ('orange', 'apple', 'grapes', 'olive', 'guava', 'lemon', 'avocado') ;
    echo '<pre>' ;
    echo ' array before : ' ;
    print_r ($myArray) ;
    $keyarray = array_keys ($myArray, 'avocado') ;
    foreach ($keyarray as $delkey) {
        unset ($myArray[$delkey]) ;
    }
    $result = array_merge($myArray) ;
    echo ' array after : ' ;
    print_r ($result) ;
    ?>
</body>
</html>
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As above now the <head> tag is used to contain information about the web page. In this tag, a <title> tag is used which helps us to specify a webpage title.
  4. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  5. Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
  6. <h1> tag used to add heading here.
  7. Now we close the HTML file with </html> tag
  8. we write <?php tag to write PHP within it.
  9. First create an array with some values.
  10. We first print the array before deleting the last element.
  11. Now creating another array to store the key value of the last element.
  12. Then using unset() function to delete the last element by the key value obtained before.
  13. Using array_merge() to merge the array
  14. Print_r() to display the final result after removing the last element.

Conclusion :-

At last, here in conclusion, here we can say that with this article’s help, we know how to remove last element from array using PHP.

I hope this article on remove last element from array PHP helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪