Open In App

How to Insert a New Element in an Array in PHP ?

Last Updated : 11 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In PHP, an array is a type of data structure that allows us to store similar types of data under a single variable. The array is helpful to create a list of elements of similar types, which can be accessed using their index or key.

We can insert an element or item in an array using the below functions:

Using array_unshift() Function

The array_unshift() Function is used to add one or more than one element at the beginning of the array. and these elements get inserted at the beginning of the array. because of the inserted elements or items in the array, the length of the array also gets increased by the number of elements inserted or added in the array. After inserting the elements, we can use the print_r() function to print the array information.

Below are examples of how to do it: Let’s see how to insert an element at the beginning of the array using the array_unshift() function.

Input:  names[] = array("portal", "for", "geeks"),
size = 3, capacity = 3
 
Output: names[] = array("_", "_", "portal", "for", "geeks"), 
size = 5, capacity = 5

Example 1: In this example, we use an indexed array to insert elements at the beginning of the array using the array_unshift() function.

PHP




<?php
$names = array("portal", "for", "geeks");
array_unshift($names, "A", "Computer science");
print_r($names);
?>


Output

Array
(
    [0] => A
    [1] => Computer science
    [2] => portal
    [3] => for
    [4] => geeks
)

Example 2: In this example, we use an associative array to insert elements at the beginning of the array using the array_unshift() function

PHP




<?php
 
// Declare an associative array
$array = array(
      "language1" => "CSS",
      "language2" => "PHP",
      "language3" => "MySQL"
);
 
array_unshift($array, "HTML");
print_r($array);
 
?>


Output

Array
(
    [0] => HTML
    [language1] => CSS
    [language2] => PHP
    [language3] => MySQL
)

Using array_push() Function

The array_push() Function function is used to push new elements into an array. We can push one or more than one element into the array and these elements get inserted to the end of the array and because of the pushed elements into the array, the length of the array also gets incremented by the number of elements pushed into the array. After inserting the elements, we use the print_r() function to print the array information.

Below are examples of how to do it: Let’s see how to insert an element at the end of the array using the array_push() function.

Input:  arr[] = array("Example", "based", "on"), 
size = 5, capacity = 5

Output: arr[] = array("Example", "based", "on", "_", "_"), 
size = 5, capacity = 5

Example 3: In this example, we use an indexed array to insert elements at the end of the array using the array_push() function.

PHP




<?php
$array_name = array("Example", "based", "on");
array_push($array_name, "PHP", "language");
print_r($array_name);
?>


Output

Array
(
    [0] => Example
    [1] => based
    [2] => on
    [3] => PHP
    [4] => language
)

Example 4: If we want to add a single element at the end of an array.

PHP




<?php
 
$array = array("Technical", "Content", "Writer");
$array[] = "at GFG";
print_r($array);
 
?>


Output

Array
(
    [0] => Technical
    [1] => Content
    [2] => Writer
    [3] => at GFG
)

Using array_merge() Function

The array_merge() Function is used to merge two or more arrays into a single array. This function is used to merge the elements or values of two or more arrays together into a single array. The merging occurs in such a manner that the values of one array are appended at the end of the previous array.

Example 5: The array_merge() function merges elements of one or more than one array into one array.

PHP




<?php
$array1 = array("GFG", "stands", "for");
$array2 = array("Geeks", "for", "Geeks");
$merge = array_merge($array1, $array2);
print_r($merge);
?>


Output

Array
(
    [0] => GFG
    [1] => stands
    [2] => for
    [3] => Geeks
    [4] => for
    [5] => Geeks
)

Using array_splice() Function

It is an advanced and extended version of the array_slice() function, where we not only remove elements from an array but can also add other elements to the array. The function generally replaces the existing element with elements from other arrays and returns an array of removed or replaced elements.

Example 6: The array_splice() method will remove elements and add other elements according to the position parameters.

PHP




<?php
 
// PHP program to illustrate the use
// of array_splice() function
 
$array1 = [
    "1" => "Geeks",
    "2" => "for",
    "3" => "geeks",
    "4" => "is",
    "5" => "fun",
];
 
$array2 = ["6" => "are", "7" => "great"];
 
echo "The returned array: \n";
print_r(array_splice($array1, 1, 4, $array2));
 
echo "\nThe original array is modified to: \n";
print_r($array1);
 
?>


Output

The returned array: 
Array
(
    [0] => for
    [1] => geeks
    [2] => is
    [3] => fun
)

The original array is modified to: 
Array
(
    [0] => Geeks
    [1] => are
    [2] => great
)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads