Introduction
This article explains adding and removing Array Elements in PHP. For this explanation I used some functions for adding and removing array elements. These functions are very important and useful to us in an array for adding and removing array elements.
Syntax
This function adds elements to the start of an array.
Array_unshift($array ,$var); |
Parameter |
Description |
Array |
the array name. |
Values |
the values or elements to be added at the start position. |
Example
<?php
$name = array("John", "Tom");
array_unshift($name, "Jony", "Richy");
echo "<pre>";print_r($name);
?>
Output
Syntax
This function removes elements from the start of an array.
Parameter |
Description |
Array |
the array name. |
Example
<?php
$name = array("John", "Tom", "Jony", "Richy");
array_shift($name);
echo "<pre>";print_r($name);
?>
Output
Syntax
This function adds elements to the end of an array.
Array_push($array ,$var); |
Parameter |
Description |
Array |
the array name |
Values |
the values or elements to be added at the last position. |
Example
<?php
$name = array("John", "Tom");
array_push($name, "Jony", "Richy");
echo "<pre>";print_r($name);
?>
Output
Syntax
This function removes elements from the end of an array.
Parameter |
Description |
Array |
the array name. |
Example
<?php
$name = array("John", "Tom", "Jony", "Richy");
array_pop($name);
echo "<pre>";print_r($name);
?>
Output