Array Push, POP PHP – PHP Array Tutorial

PHP Array Push and Pop - Lara Tutorials

In PHP, the array methods like array_push and array_pop is used to add or remove the elements or items from the array.

In PHP, The array_push method can use to add or insert one or more items or elements from the end of an array. and The array_pop() method can use to extract, delete and remove the elements/items from the end of the array.

Here we will discuss about PHP push array and pop array method with its definition, syntax , examples.

PHP array_push() function

The PHP array_push() function is used to add or insert one or more elements to the end of an array.

Syntax

array_push(array, value1, value2, …)

Example – add/insert/push elements/items in an array PHP

In this example, we have one array “array(“PHP”, “laravel”, “codeigniter”)”, it contains value like (“PHP”, “laravel”, “codeigniter”). If we want to add/push one or more values in the array. You can add/push the values into array see below examples:

Here we will add new values like(“WordPress”,”bootstrap”,”HTML”) in existing array using PHP array_push() function:

<?php
 
 $array = array("php", "laravel", "codeigniter");
 
 //before adding new values 
 echo "Before add the value:- ";
 print_r($array); echo "<br>";
 
 //add elements/values in array
 array_push($array,"wordpress","bootstrap","html");
 
 //after adding a new values
 echo "After add the value:- ";
 print_r($array);
  
?>

PHP array_pop() function

The PHP array_pop() array function is used to remove or delete elements to the end of an array.

Syntax

array_pop(array);

Example – Delete/Remove elements/items from an array PHP

In this example, we have one array “array(“PHP”, “laravel”, “codeigniter”,”bootstrap”)”, it contains value like (“PHP”, “laravel”, “codeigniter”,” bootstrap “). If we want to remove/delete elements in the array. You can removes/deletes the elements/values into array see below examples:

<?php
 
 $array = array("php", "laravel", "codeigniter","bootstrap");
 
 //before remove elements array
 echo "Before add the value:- ";
 print_r($array); echo "<br>";
 
 //remove elements from array
 array_pop($array);
 
 //after remove elements from array
 echo "After add the value:- ";
 
 print_r($array);
  
?>

Conclusion

Array push and pop in PHP. Here you have learned how to insert/add/push, remove/delete/pop elements/items in array PHP.

Leave a Reply

Your email address will not be published.