Array Push and POP in PHP | PHP Tutorial

Array Push and POP in PHP | PHP Tutorial

Array push and pop in PHP. We will learn how to use the array push and array pop method in PHP.

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.

If you want to know more about the how-to add/insert/ elements/values in array PHP, PHP array push with key, PHP adds to an associative array, PHP add to the multidimensional array, array push associative array PHP, PHP array add key-value pair to an existing array. Click here

If you want to know more about PHP array pop function, like how to remove array inside array PHP, remove an element from array PHP, remove the key from associative array PHP, remove array from multidimensional array PHP, how to remove the particular key from an array in PHP, remove array inside array PHP. Click here

Leave a Reply

Your email address will not be published. Required fields are marked *