PHP Delete Array Element with Built-in Function

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.

How to delete PHP array element
How to delete PHP array element

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.

array_pop($array_name);

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.

array_shift($array_name);

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.

I am a system administrator and like to share knowledge that I am learning from my daily experience. I usually work on MikroTik, Redhat/CentOS Linux, Windows Server, physical server and storage, virtual technology and other system related topics. Web development is also my favorite topic and love to work with PHP, HTML, CSS, Tailwindcss, Bootstrap, JavaScript, jQuery, Vue.js, WordPress and Laravel framework. Follow Me: Facebook, Twitter and Linkedin.

Related Posts

Laravel First Time Installation on Windows 10/11

Laravel is one of the best full stack frameworks used to develop modern web application simply and securely. Day by day PHP developers are absorbing Laravel. So,…

PHP Add Element to Array with Built-in Functions

Array plays an important role in PHP programming. It is one of the most used data structures in PHP programming. So, it is better to have a…

PHP Array Explanation

Array is one of the most important and basic terms in PHP Language. Almost all the PHP program uses array data structure. So, those who want to…

Function Call by Value and Call by Reference in PHP

More than thousands of built-in functions proves the power of PHP Language. Besides built-in function PHP allows to create user defined function. How to create user defined…

PHP Variable Usage inside of Function

Besides more than thousands of built-in functions PHP provides facility to define custom/user defined function. How to define and call a user defined function was discussed in…

PHP Function Explanation

Real power of a programming language comes from its functions. More than thousands of built-in functions defined in PHP language proves the power of PHP language. Besides…

Leave a Reply

Your email address will not be published.

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.