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:
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
1 2 3 4 5 6 7 8 |
<?php $my_arr = array('Saumya', 'Nikita', 'Upasana', 'Dyana'); // use array_push to add 3 elements to end of $my_arr $new_num = array_push($my_arr, 'Adhikar', 'sanjay', 'Puspendra', 'Mukesh'); // inspect return value of array_push echo $new_num; print_r($my_arr); ?> |
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
1 2 3 4 5 6 7 8 |
<?php $my_arr = array('Saumya', 'Nikita', 'Upasana', 'Dyana'); // use array_unshift to add 3 elements to end of $my_arr $new_num = array_unshift($my_arr, 'Adhikar', 'sanjay', 'Puspendra'); // inspect return value of array_unshift echo $new_num; print_r($my_arr); ?> |
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
1 2 3 4 5 6 7 8 9 10 |
<?php $my_arr = array('Apple', 'Apricots', 'Avocado', 'Banana'); // integer index in square brackets $my_arr[4] = 'Blackberries'; // string index in square brackets $my_arr['Blackcurrant'] = 'Breadfruit'; // square brackets without index $my_arr[] = 'Cherimoya'; print_r($my_arr); ?> |
[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
1 2 3 4 5 6 7 |
<?php $my_arr = array('Apple', 'Apricots', 'Avocado', 'Banana'); // array_splice arguments: array to modify, offset (where to insert), // number of elements to remove, array of elements to add array_splice($my_arr, 2, 0, ['Blackberries', 'Breadfruit'] ); print_r($my_arr); ?> |
[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:
1 2 3 4 5 6 |
<?php $my_arr = array('Apple', 'Apricots', 'Avocado', 'Banana'); // array_splice with negative offset array_splice($my_arr, -1, 0, ['Blackberries', 'Breadfruit'] ); print_r($my_arr); ?> |
[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.
1 2 3 4 5 6 7 8 9 10 11 |
<?php $my_arr1 = array("India", "New Delhi"); $my_arr2 = array("Swaroop", "Nagar"); // Get the merged array in the first array itself. $my_arr1 = array_merge($my_arr1, $my_arr2); echo "New Contents: "; // Use for each loop to print all the array elements. foreach ($my_arr1 as $value) { echo $value . "\n"; } ?> |
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:
1 2 3 4 5 6 |
<?php $Technical_tutorials = array("PHP", "HTML", "Wordpress"); // Append array with new items array_push($Technical_tutorials, "Javascript", "Core Java"); print_r($Technical_tutorials); ?> |
[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).
1 2 3 4 5 |
<?php $course = ['HTML', 'Python', 'SQL Server', 'Javascript', 'jquery']; array_unshift($course, 'SQL'); print_r($course); ?> |
[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!