This PHP tutorial help to add an element or array of elements into an array. PHP has a number of built-in functions to add elements into an array. We can combine two or more arrays to create a new (all-combination) array.
Pushing a new element into an array, adding one array to another array, merging two or more arrays together, and so on are all examples of array append.
In a PHP application, there are the following real-time conditions in which you must add elements to an array –
- Add elements into the empty array.
- Add elements into the existing array.
- Add elements to the End of an Array in PHP.
- Add elements at the beginning of an array in PHP.
Also checkout other related tutorials,
- Merge Two Array or Multiple Array in PHP
- How To Convert XML To Associative Array in PHP
- Remove Duplicates From Multidimensional Array
- Insert PHP Array into MySQL Table
Let’s discuss one by one scenario to add items into an array –
Add Elements into the Empty array in PHP
The following methods can be used to add single or multiple elements to an empty array:
$arr = array(1); //multiple elements $arr = array(1, 2, 3); //OR $arr[] = 1; $arr[] = 2; echo "<pre>";print_r($arr);
Output :
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 1 [4] => 2 )
Add elements into the existing array in PHP
You can add items to the existing array using the following ways into PHP –
//Numeric key $arr[3] = 4; echo " <pre>";print_r($arr); //Text key (assoc) $arr['key'] = 'phpflow'; print_r($arr);
Output :
Array ( [3] => 4 ) Array ( [3] => 4 [key] => phpflow )
Add Elements Using array_unshift()
The PHP’s array_unshift() function is used to add elements to the beginning or starting of an array. It takes two arguments one is the array and the other is any number of elements you would like to add to the array.
$arr = array(); array_unshift($arr, 1); array_unshift($arr, 2); echo " <pre>";print_r($arr); // Or array_unshift($arr, 3, 4); print_r($arr);
Output:
Array ( [0] => 2 [1] => 1 ) Array ( [0] => 3 [1] => 4 [2] => 2 [3] => 1 )
How to add elements to the end of an array in PHP
The array_push() function to insert one or more elements or values at the end of an array. You can add items at the end of an array in php using array_push
method :
$arr = array(); array_push($arr, 1); array_push($arr, 2); echo " <pre>";print_r($arr); // Or array_push($arr, 3, 4); print_r($arr);
Output:
Array ( [0] => 1 [1] => 2 ) Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )
Append Elements to Array Using array_splice
The array_splice function can be used to add elements, remove elements, and/or replace elements in an array.
Syntaxarray_splice(array, start, length, array)
- array: Required! Specifies an array
- start: Required! Where the function will start to insert or remove elements.
- length: Required! How many elements will be inserted/removed.
- array: Optional! inserted to the original array.
$my_arr = array(1, 4, 6, 7); array_splice($my_arr, 2, 0, [11, 13] ); print_r($my_arr);
Output:
Array ( [0] => 1 [1] => 4 [2] => 11 [3] => 13 [4] => 6 [5] => 7 )