Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to insert a new element in an array ?

  • Difficulty Level : Basic
  • Last Updated : 22 Nov, 2021

In PHP, an array is a type of data structure that allows us to store similar type 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 two functions which is:

  • array_unshift() function
  • array_push() function

Using array_unshift() function This 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() functionThis 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() functionThe 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 element of one or more than one arrays 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
)

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :