Here are the various ways to insert values in a PHP array.
- Insert value at the end.
- Insert value at the start.
- Insert value with a key.
- array push if not exists.
- Insert value in a multidimensional array.
1. PHP array_push() function
The PHP array_push() function is used to insert elements at the end of an array. This function takes two or more parameters –
array
– The first parameter is an array.$values
– and after the array, you can pass multiple values as parameters to insert into the array.
<?php
array_push(array, $value1, $value2, $value3, ...);
<?php
$fruits = ["Apple", "Banana", "Watermelon"];
array_push($fruits, "Strawberry", "Pineapple");
print_r($fruits);
Array
(
[0] => Apple
[1] => Banana
[2] => Watermelon
[3] => Strawberry
[4] => Pineapple
)
2. Insert values at the start of a PHP array
The PHP array_unshift()
function is similar to the array_push()
function, but it inserts values at the start of an array.
<?php
$fruits = ["Apple", "Banana", "Watermelon"];
array_unshift($fruits, "Strawberry", "Pineapple");
print_r($fruits);
Array
(
[0] => Strawberry
[1] => Pineapple
[2] => Apple
[3] => Banana
[4] => Watermelon
)
3. PHP array insert values with keys
Use the following method to insert values into a PHP array with keys.
<?php
$user = ["name" => "John", "age" => 22];
$user["email"] = "[email protected]";
print_r($user);
Array
(
[name] => John
[age] => 22
[email] => [email protected]
)
4. PHP array push if not exists
Insert if the value does not exist
Use the in_array()
function to check whether a value exists in a PHP array or not.
<?php
$fruits = ["Apple", "Banana", "Strawberry"];
function array_insert_if_value_not_exist(&$arr, ...$values)
{
foreach ($values as $v) {
if (!in_array($v, $arr)) {
array_push($arr, $v);
}
}
}
array_insert_if_value_not_exist($fruits, "Pineapple", "Apple");
print_r($fruits);
Array
(
[0] => Apple
[1] => Banana
[2] => Strawberry
[3] => Pineapple
)
Insert if the key does not exist
Use the isset()
function to check whether a key exists in a PHP array or not.
<?php
$user = ["name" => "John"];
function insert_if_key_not_exist(&$arr, $key, $val){
if(!isset($arr[$key])){
$arr[$key] = $val;
}
}
insert_if_key_not_exist($user, "name", "Mark");
insert_if_key_not_exist($user, "age", 22);
insert_if_key_not_exist($user, "email", "[email protected]");
print_r($user);
Array
(
[name] => John
[age] => 22
[email] => [email protected]
)
5. Insert value in a multidimensional array in PHP
The following examples will do the same thing. Use one of the following two ways to insert value in a multidimensional array.
<?php
$data = [
"fruits" => ["Apple", "Banana"],
"vegetables" => ["Potato", "Tomato"]
];
array_push($data["fruits"], "Pineapple");
array_push($data["vegetables"], "Onion");
print_r($data);
<?php
$data = [
"fruits" => ["Apple", "Banana"],
"vegetables" => ["Potato", "Tomato"]
];
$data["fruits"][] = "Pineapple";
$data["vegetables"][] = "Onion";
print_r($data);
Array
(
[fruits] => Array
(
[0] => Apple
[1] => Banana
[2] => Pineapple
)
[vegetables] => Array
(
[0] => Potato
[1] => Tomato
[2] => Onion
)
)