PHP Add to Array | How to Add Elements to The End & Start of an Array

Summary: On this page we demonstrate and describe How to use PHP array_push() Function and the various ways that you can Add Elements to The End and Starting of an Array in PHP. We cover square bracket syntax as well as the array_unshift functions. The array_splice function, which is also used to add elements. let’s understand in Details.

PHP Add to Array Overview:

This tutorial covers a few things regarding PHP Add to Array:

TABLE OF CONTENT
1. Using PHP array_push() Function
2. Using PHP array_unshift Function
3. Using Square Bracket Syntax
4. Using array_splice Function
5. Using PHP array_merge Function
6. How to add elements to the end of an array in PHP
7. How to Add element at the start of the Array in PHP

1. Using PHP array_push() Function

You can use PHP’s array_push function to add multiple elements to the end of an array, or values at the end of an array. therefore, pass the array as the 1st argument followed by any number of elements in the order in which you would like them to be added. array_push() function in PHP will returns the number of elements in the modified array. We demonstrate by adding four new elements to the example array, Let’s try out an example and see how this function works:

Program

8
Array (
[0] => Saumya
[1] => Nikita
[2] => Upasana
[3] => Dyana
[4] => Adhikar
[5] => sanjay
[6] => Puspendra
[7] => Mukesh
)

2. Using PHP array_unshift Function

The PHP’s array_unshift function is used to add elements to the beginning or staring of an array. As with PHP array_push, pass the array first, followed by any number of elements you would like to add to the array. Arrays with numeric indexes have those indexes re-numbered starting from 0 (zero). Here, in this program, we demonstrate adding three elements to the array:

Program

7
Array (
[0] => Adhikar
[1] => sanjay
[2] => Puspendra
[3] => Saumya
[4] => Nikita
[5] => Upasana
[6] => Dyana )

3. Using Square Bracket Syntax

suppose that when you wish or need to add individual elements to the end of an array in PHP, then the square bracket syntax is the most accomplished. therefore, you can include an index (string or integer) in the square brackets, or leave them empty, therefore, in which case PHP will use the next available integer. We demonstrate each of these options here:

Program

Array (
[0] => Apple
[1] => Apricots
[2] => Avocado
[3] => Banana
[4] => Blackberries [Blackcurrant] => Breadfruit
[5] => Cherimoya
)

4. Using PHP array_splice Function

The array_splice function can be used to add elements, remove elements, and/or replace elements in an array. this function modifies the array passed as its 1st argument, and it returns an array of removed elements. therefore, the modified array is re-indexed starting from 0 (zero). An offset argument allows specifying wherein the array to make changes.

The given below example demonstrates that how to use the PHP array_splice function to insert new elements anywhere you like in an existing array:

Program

Array (
[0] => Apple
[1] => Apricots
[2] => Blackberries
[3] => Breadfruit
[4] => Avocado
[5] => Banana
)

Suppose that when you wish or want to use the array_splice function in PHP to add elements to an array, but not to remove or replace, then pass 0 as the 3rd argument because this function supports a negative offset which allows or permit you to specify a location starting or beginning from the end of the array. The given below program demonstrates how to insert two elements one element from the end of the array:

Array (
[0] => Apple
[1] => Apricots
[2] => Avocado
[3] => Blackberries
[4] => Breadfruit
[5] => Banana
)

5. Using PHP array_merge Function : PHP append one array to another

PHP array_merge Function using to merge function returns a new array after merging the two arrays.

New Contents: India New Delhi Swaroop Nagar

PHP Add to Array Related Question and Answer

6. How to add elements to the end of an array in PHP

Answer: If you wish to add elements to the end of an array in PHP then use the PHP array_push() function to insert one or more elements or values at the end of an array. Let’s see the program and check that how this function works:

Array (
[0] => PHP
[1] => HTML
[2] => WordPress
[3] => Javascript
[4] => Core Java )

7. How to Add element at the start of the Array

Answer: You want to add element at the start or beginning of the Array then you can use the PHP array_unshift() function. therefore, it appends the item at the starting of the array at the index of 0 (zero).

Array (
[0] => SQL
[1] => HTML
[2] => Python
[3] => SQL Server
[4] => Javascript
[5] => jquery
)

Conclusion:

In this article, you have learned PHP array_push() Function and the various ways that you can Add Elements to The End and Starting of an Array in PHP and also learned the square bracket syntax as well as the array_unshift functions. The array_splice function, which is also used to add elements. I hope you will enjoy it!. if have any query related program then contact me at info@tutorialscan.com I will try to resolve the problem. Stay healthy, stay safe! Happy coding! enjoy it!