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 better understanding on how to declare and how to use PHP array in PHP coding.

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 am going to 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 PHP Arrays

Adding Element at the End of a PHP Array

PHP has thousands of built-in functions 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 a PHP array_push() function is:

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 be inserted. If the provided 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 the array_unshift() function is:.

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

Like the array_push() function, the first argument of the array_unshift() function must be an array where new elements will be inserted and other arguments will be 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() built-in 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 confusion on 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. 

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

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…

PHP break, continue and goto Statements

PHP break and continue statements are used to dominate or control PHP loop statements and PHP goto statement is used to jump one execution label to another execution label….

PHP Loop Statements Explanation

Loop statement is one of the most important topics in PHP programming language. Like other programming languages, PHP loop statements are frequently used in PHP programming. So,…

Leave a Reply

Your email address will not be published.

*

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