Learn
Ordered Arrays
More Array Methods: Pushing and Popping

In the previous exercise, we learned how to add single array elements and to change array elements at a given index. PHP also provides us with built-in methods for removing array elements, and for adding many elements at once.

The array_pop() function takes an array as its argument. It removes the last element of an array and returns the removed element.

$my_array = ["tic", "tac", "toe"]; array_pop($my_array); // $my_array is now ["tic", "tac"] $popped = array_pop($my_array); // $popped is "tac" // $my_array is now ["tic"]

Note that array_pop() doesn’t just set the last element to NULL. It actually removes it from the array, meaning that array’s length will decrease by one (which we can verify using count()).

The array_push() function takes an array as its first argument. The arguments that follow are elements to be added to the end of the array. array_push() adds each of the elements to the array and returns the new number of elements in the array.

$new_array = ["eeny"]; $num_added = array_push($new_array, "meeny", "miny", "moe"); echo $num_added; // Prints: 4 echo implode(", ", $new_array); // Prints: eeny, meeny, miny, moe

Let’s practice!

Instructions

1.

We provided you an array $stack. Use the array_push() function to add two elements to the array: "blocker" and "impediment".

2.

Use the array_pop() function to remove all but one element from the array.

Folder Icon

Take this course for free

Already have an account?