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>
- First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
- Secondly, the <html> tag is used to indicate the beginning of an HTML document.
- 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.
- Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
- Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
- <h1> tag used to add heading here.
- Now we close the HTML file with </html> tag
- we write <?php tag to write PHP within it.
- First create an array with some values.
- We first print the array before deleting the last element.
- Using the array_pop() method to remove the last element.
- 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>
- First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
- Secondly, the <html> tag is used to indicate the beginning of an HTML document.
- 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.
- Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
- Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
- <h1> tag used to add heading here.
- Now we close the HTML file with </html> tag
- we write <?php tag to write PHP within it.
- First create an array with some values.
- We first print the array before deleting the last element.
- Now creating another array to store the key value of the last element.
- Then using unset() function to delete the last element by the key value obtained before.
- Using array_merge() to merge the array
- 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.