Adding and Removing Array Elements in PHP

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

 add1.jpg

Syntax

This function removes elements from the start of an array.

Array_shift($array);

 

Parameter Description
Array the array name.

Example

<?php

$name = array("John", "Tom", "Jony", "Richy");

array_shift($name);

echo "<pre>";print_r($name);

?>

 

Output

 add3.jpg

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

add2.jpg 

Syntax

This function removes elements from the end of an array.

Array_pop($array);

 

Parameter Description
Array the array name.

 

Example

 

<?php

$name = array("John", "Tom", "Jony", "Richy");

array_pop($name);

echo "<pre>";print_r($name);

?>

Output

 add4.jpg


Similar Articles