PHP Array Push and Pop

Array push and pop in PHP; Through this tutorial, i am going to show you how to use the array push and array pop method in PHP.

PHP Array Push and Pop

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

Now i will take following examples using array_push and array_pop() function in PHP; is as follows:

PHP array_push() function

Using php array_push function, you can add or insert one or more elements to the end of an array.

Let’s see the following example to add or insert one or more elements to the end of an array; is as follows:

<?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

Using php array_pop function, you can remove or delete one or more elements to the end of an array.

Let’s see the following example to delete or remove one or more elements to the end of an array; is as follows:

<?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. Through this tutorial, you have learned how to insert/add/push, remove/delete/pop elements/items in array PHP.

Recommended PHP Tutorials

Leave a Comment