PHP Add Element to Array with Built-in Functions

Array plays an important role in PHP programming. It is one of the mostly used data structures in PHP programming. How to declare PHP array and how to deal with various array was discussed in the previous article. Array elements can be added manually or using PHP built-in array functions. It is so easy to add PHP array elements with PHP built-in function. So, this article I will show how to add PHP array elements using built-in array functions.

How to Add Elements to a PHP Array
How to Add Elements to a PHP Array

Add Element at the End of a PHP Array

PHP has thousands of built-in function for various purposes. Among them, the array_push() function is used to add one or more elements at the end of an array. The structure of PHP array_push() function is given below.

array_push($array_name,”Element to add”);

The first argument of the array_push() function is an array where new elements will be added and other arguments will be desired array elements those will inserted. If the given array already contains any element, the new elements will be added at the end of that array.

See the following example where the array_push() function is being used to add elements at the end of a PHP array.

<?php  
 $web = array("PHP","HTML","CSS"); 
 array_push($web,"JavaScript","AJAX");  
  foreach($web as $lang){
       echo $lang." ";
  } 
?>

Output: PHP HTML CSS JavaScript AJAX

Here, two new elements has been added at the end of the $web array using the array_push() function and then the array elements are being retrieved using PHP foreach loop statement.

Add Element at the beginning of an Array

Besides adding array element at the end, it is also possible to add element at the beginning of a PHP array using PHP built-in function. The array_unshift() function is a built-in function which is used to add elements at the beginning of a PHP array. The structure of array_unshift() function is given below.

array_unshift($array_name,”Element to add”);

Like array_push() function, the first argument of array_unshift() function must be an array where new elements will be inserted and other arguments are desired new array elements those will be added at the beginning of this array.

See the following example where the array_unshift() function is being used to add elements at the beginning of a PHP array.

<?php
 $web = array("PHP","HTML","CSS");
 array_unshift($web,"JavaScript","AJAX");
  foreach($web as $lang){
   echo $lang." ";
  }
?>

Output: JavaScript AJAX PHP HTML CSS

Here, two new elements has been added at the beginning of the $web array using the array_unshift() function and then array elements are being retrieved using PHP foreach loop statement.

How to add array elements at the beginning or at the end of an array using PHP built-in array function has been explained in this article. I hope you will now be able to add PHP array elements using PHP built-in function so easily. However, if you have any question about PHP array_push() or array_unshift() function, feel free to discuss in comment or contact me from Contact page. I will try my best to stay with you. 

Related post:

php-add-element-to-array-with-built-in-functions

ABU SAYEED

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, JavaScript, AJAX, jQuery, WordPress and CodeIgniter framework. Follow Me: Facebook, Twitter and Linkedin.

Your name can also be listed here. Have an IT topic? Submit it here to become a iTechSheet author.