Array is an important and mostly used data type in PHP programming. PHP programmer can manually add element to an array or delete element from an array with his/her own logical program but PHP also provides some built-in functions which help to delete array elements so easily.
In my previous article, I explained how to add element to an array using PHP built-in functions. In this article, I am going to explain how to delete array element using PHP built-in functions.

Delete Last Element of a PHP Array
PHP array_pop() function is used to delete the last element of an array. The structure of the array_pop() function is given below.
The array_pop() function only accepts the name of an array and then it will remove the last element of that array.
The following program is a simple example of the PHP array_pop() function where the array_pop() function is being used to remove the last element of a PHP array.
<?php $web = array("PHP","HTML","CSS","JavaScript","AJAX"); array_pop($web); foreach($web as $lang){ echo $lang." "; } ?>
Output: PHP HTML CSS JavaScript
The example shows that the last element of $web array has been removed by the array_pop() function and the remaining elements are being shown using foreach loop statement.
Delete Array First Element
PHP array_shift() function is used to remove the first element of an array. The structure of the array_shift() function is given below.
The array_shift() function only accepts the array name as an argument and then remove the first element of that array.
The following program is a simple example of the array_shift() function where the array_shift() function is being used to remove the first element of a PHP array.
<?php $web = array("PHP","HTML","CSS"); while(count($web)){ $shift_element= array_shift($web); print("Deleted: ".$shift_element." "); print("Now array contains: ".count($web)." element "); } ?>
Output
Deleted: PHP
Now array contains: 2 element
Deleted: HTML
Now array contains: 1 element
Deleted: CSS
Now array contains: 0 element
From the above example we can see that the first element of the $web array has been removed by the array_pop() function in every successful looping.
Removing array elements using PHP built-in array function has been explained in this article. I hope you will now be able to manage your PHP array properly. However, if you face any confusion to understand the PHP array function, feel free to discuss in comment or contact me from Contact us page. I’ll try my best to stay with you.
Sorting PHP array elements is another important array operation in PHP programming. PHP array sorting will be discussed in the next article. Hope it will be helpful for you.