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 –
Also checkout other related tutorials,
Let’s discuss one by one scenario to add items into an array –
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 )
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 )
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 )
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 )
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)
$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 )
This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More
in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More