Using php array_push example you will learn how to add values or elements with key to an array in PHP. We have covered below examples how to add or push the values with key to an array in PHP.
We have added how to push in an empty array or exitings and also how to insert elements with key and value to an array easy way.
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.
Here we have added examples, like add values in an empty array, array push with key, how add key and value in an associative array, array_push in multidementional array in php.
Let’s know about the PHP array_push() function, like array_push function definition, syntax, and examples:
PHP array_push() function
PHP array_push() function inserts one or more elements to the end of an array. The array_push() method takes a single element or an array of elements and appends it to the array.
Syntax:
array_push(array, value1, value2, ...)
Example 1: add values in an array PHP
<?php
$array = array("php", "laravel", "codeigniter");
array_push($array, "wordpress", "yii", "symphony");
//after adding a new values
print_r($array);
?>
Output:
Array (
[0] => php
[1] => laravel
[2] => codeigniter
[3] => wordpress
[4] => yii
[5] => symphony
)
Example 2: Add elements to an empty array in php
<?php
$array = array();
array_push($array, "php", "laravel", "codeigniter");
print_r($array);
?>
Output:
Array (
[0] => php
[1] => laravel
[2] => codeigniter
)
Example 3: Adding array into an array in PHP
Using this example the new array generate with new key 0, 1 etc. If you want to key and values insert of your static then go below exmple:
<?php
$array1 = [ 'name' => 'John', 'department' => 'Developer' ];
$array2 = ['Facebook', 'Instagram'];
$newArray = array_push($array1, $array2);
print_r($newArray);
Output:
Array (
[a] => red
[b] => green
[0] => Array (
[0] => Facebook
[1] => Instagram
)
)
Example 4: Add Key and Values an array in PHP
Here we will push the values in array with key without using array function:
<?php
$array = array("a"=>"red","b"=>"green");
$array['c'] = "yellow";
$array['d'] = "brown";
print_r($array);
Array (
[a] => red
[b] => green
[c] => yellow
[d] => brown
)
Example 5: Adding element at the start of the Array
To add an element at the start of the array, you can use the PHP array_unshift() function. It appends the item at the beginning of the array at the index of 0.
<?php $data = ['Python', 'Javascript', 'Golang']; array_unshift($data, 'PHP'); print_r($data);
Output:
Array
(
[0] => PHP
[1] => Python
[2] => Javascript
[3] => Golang
)
Example 5: PHP append one array to another
Now, we will take example of push one array to another array or push array into an array without using array_push() function.
<?php
$a = array('a', 'b');
$b = array('c', 'd');
$merge = array_merge($a, $b);
?>
Example 6: php push array in multidimensional array
<?php
$array = [
'web' => ['html', 'css', 'bootstrap'],
'p_lang' => ['php', 'python', 'cabbage'],
'framework' => ['laravel', 'codeigniter']
];
$array1['test'] = array('laravel', 'codeigniter');
array_push($array['framework'], $array1);
print_r($array);
?>