Add elements/items to an array in PHP; In this tutorial, we will learn how to add or push the items/elements to array in PHP.
Through this tutorial, we will learn the following topics to add elements/items to array in PHP:
- PHP array push with key
- array push associative array PHP
- PHP add to multidimensional associative array
- PHP add to multidimensional array
- How to push array in multidimensional array
- PHP append one array to another
Now, we will take some examples, like add values in array PHP, PHP array push with key, PHP add to an associative array, PHP add to the multidimensional array, array push associative array PHP, PHP array add key-value pair to an existing array.
Add or Insert elements/values to array In PHP
You can use PHP array_push() function for adding one or more elements/values to the end of an array.
Let’s know about the PHP array_push() function, like array_push function definition, syntax, and examples:
PHP array_push() function
The PHP array_push() function is used to add one or more elements to the end of an array.
Syntax
array_push(array, value1, value2, …)
Example 1 – add values in array PHP
In this example, we have one array “array(“PHP”, “laravel”, “codeigniter”)”, it contains value like (“PHP”, “laravel”, “codeigniter”). If we want to add/push one or more values in the array. You can add/push the values into array see below examples:
Here we will add new values like(“WordPress”,”bootstrap”,”HTML”) in existing array using PHP array_push() function:
<?php
$array = array("php", "laravel", "codeigniter");
//before adding new values
echo "Before add the value:- ";
print_r($array); echo "<br>";
//add elements/values in array
array_push($array,"wordpress","bootstrap","html");
//after adding a new values
echo "After add the value:- ";
print_r($array);
?>
PHP array push with key
Now we have one new array like this ” $array = array(“a”=>”red”,”b”=>”green”); “. If we want to add the values into array with key. You can use the below code:
// add the values in array without using array function $array['c'] = "yello"; $array['d'] = "brown";
Here we will push the values in array with key without using array function:
<?php
$array = array("a"=>"red","b"=>"green");
//before adding new values
echo "Before add the value:- ";
print_r($array);
echo "<br>";
// add the values in array without using array function
$array['c'] = "yello";
$array['d'] = "brown";
//after adding a new values
echo "After add the value:- ";
print_r($array);
?>
PHP add to multidimensional array
If we want to add values/elements in a multi-dimensional array. Here we will take an example for adding the values/elements in a multidimensional array.
If you have a multidimensional array like this:
$array = [ 'web' => ['html', 'css', 'bootstrap'], 'p_lang' => ['php', 'python', 'cabbage'], 'framework' => ['laravel', 'codeigniter'] ];
And you want to add values/elements inside the array elements. You can use the below example for adding the values/elements in the multidimensional array:
<?php
$array = [
'web' => ['html', 'css', 'bootstrap'],
'p_lang' => ['php', 'python', 'cabbage'],
'framework' => ['laravel', 'codeigniter']
];
//before adding new values
echo "Before add the value:- ";
print_r($array); echo "<br>";
//add elements/values in array
array_push($array['framework'], 'wordpress', 'joomla');
print_r($array);
//after adding a new values
echo "After add the value:- ";
print_r($array);
?>
How to push array in multidimensional array
Here we will take an example with the multi-dimensional array. In this example, we will push new array into multidimensional-array.
Let’s see the example below:
<?php
$array = [
'web' => ['html', 'css', 'bootstrap'],
'p_lang' => ['php', 'python', 'cabbage'],
'framework' => ['laravel', 'codeigniter']
];
$array1['test'] = array('laravel', 'codeigniter');
//before adding new values
echo "Before add the value:- ";
print_r($array); echo "<br>";
//add elements/values in array
array_push($array['framework'], $array1);
print_r($array);
//after adding a new values
echo "After add the value:- ";
print_r($array);
?>
PHP append one array to another | PHP push array into an array
Now, we will take example of push one array to another array or push array into an array without using array_push() function.
Add one array into another array in PHP:
<?php
$a = array('a', 'b');
$b = array('c', 'd');
$merge = array_merge($a, $b);
?>
Conclusion
Through this tutorial, we have learned how to add values in array PHP, PHP array push with key, PHP add to an associative array, PHP add to the multidimensional array, array push associative array PHP, PHP array adds key-value pair to an existing array with examples.
Recommended PHP Tutorials
- PHP Array: Indexed,Associative, Multidimensional
- To Remove Elements or Values from Array PHP
- PHP remove duplicates from multidimensional array
- Remove Duplicate Elements or Values from Array PHP
- How to Convert String to Array in PHP
- Array Push and POP in PHP | PHP Tutorial
- PHP Search Multidimensional Array [key and value and return key]
- PHP Array to String Conversion – PHP Implode