PHP array_pop() and array_push() Tutorial

In this tutorial, you will learn how to use PHP’s array push and array pop method.

How to use array_pop() and array_push() methods in PHP

Here are:

PHP array_push() function

The array_push() function adds one or more elements to the end of an array. It takes two arguments: the first argument is the array to which the elements will be added, and the remaining arguments are the elements to be added.

Syntax of array_push() function

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 array_pop() function removes the last element from an array and returns it. It takes one argument, which is the array from which the element will be removed.

Syntax of PHP array_pop() function

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

Recommended PHP Tutorials

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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