❮ PHP Array Reference Show
ExampleInsert "blue" and "yellow" to the end of an array: <?php Try it Yourself » Definition and UsageThe array_push() function inserts one or more elements to the end of an array. Tip: You can add one value, or as many as you like. Note: Even if your array has string keys, your added elements will always have numeric keys (See example below). Syntaxarray_push(array, value1, value2, ...) Parameter Values
Technical Details
More ExamplesExampleAn array with string keys: <?php Try it Yourself » ❮ PHP Array Reference The PHP array_pop() function pops (deletes) the last element of the given array, and returns the value of popped item. Table of Contents The original array we pass to array_pop() gets modified. It loses the last item after array_pop() execution. Syntax – array_pop()The syntax of array_pop() function is array_pop( array) where
Function Return Value array_pop() returns the popped item. In case, if the array is empty, array_pop() returns NONE. Example 1: Pop the Last Element of ArrayIn this example, we will take an array with four items. We call array_pop() function with the array passed as argument. We’ll then store the value returned by array_pop() function and print it. Also, as the original array gets modified, with its last item no more present, we will print this array as well, to see the contents. PHP Program <?php $array1 = array("apple", "banana", "mango", "orange"); $key = array_pop($array1); echo $key; echo "<br>"; print_r($array1) ?> Output Example 2: Pop Elements of Array Iteratively in LoopIn this example, we will take an array with four items as in previous example. We will use a for loop to iteratively pop items from the array. PHP Program <?php $array1 = array("apple", "banana", "mango", "orange"); while(sizeof($array1)>0) { echo array_pop($array1); echo "<br>"; } ?> Output ConclusionIn this PHP Tutorial, we learned how to , using PHP Array array_pop() function. To remove the last element or value from an array, array_pop() function is used. This function returns the last removed element of the array and returns NULL if the array is empty, or is not an array. After removing the last element from the array, the array is modified, and the index is reduced by one from last, only if the keys are numerical. It is an inbuilt array function of PHP, which deletes the last element of an array. Returns ValueThe array_pop() function, which is used to remove the last element from an array, returns the removed element. It also returns NULL, if the array is empty, or is not an array. For example: Using string elementsOutput An element "Skoda" is removed from the end of the given array, and the updated list is displayed in the given output. Arraylist: Array ( [0] => Mercedes [1] => Creta [2] => Audi [3] => Chevrolet [4] => Skoda ) Removed element from array is: Swift Updated arraylist: Array ( [0] => Mercedes [1] => Creta [2] => Audi [3] => Chevrolet ) Example: Using numeric keys Output Removed element: Jupiter Array ( [1] => DLF [2] => GIP ) Example: Using numeric values Output An element 39 is removed from the end of the given array, as you can see in the below output. Removed array element is: 39 Update array is: Array ( [0] => 45 [1] => 76 [2] => 23 [3] => 91 [4] => 82 ) PHP array pop is a function that allows deleting the last element of an array while saving its value. Certainly, the given function directly adds to maintaining the required length of an array. Hence, this post pens down the syntax of the PHP array pop function along with its working criteria, and uses. So, continue reading to trim down your arrays while saving the data for future use. Contents
What Is PHP Array Pop?PHP array pop is a function that removes the last element of an array. Furthermore, the stated function accepts an array, be it an associative, indexed, or multidimensional array, and trims it down. Next, the PHP array pop function returns the value that it has recently deleted from the array. Hence, you won’t end up losing the data after removing it from the given array. Furthermore, this is the syntax for the PHP array pop: array_pop(array). – Coding ExampleFor example, let’s assume that you were asked to create an array of numbers but you accidentally added an extra number into your array. So, now you’ll use the PHP array pop function to remove the last element from the array and save it in another variable. Here is a code snippet to help you come out of the trouble: <?php You’ll get the following output on your browser when you run this code: 100 Array ( [0] => 20 [1] => 40 [2] => 60 [3] => 80 ) – NoteThe PHP array pop function will return nothing when the given array is empty. However, it will display a warning when the argument passed to it is not an array. Does It Remove the Whole Inner ArrayCertainly, you’ve got a clear mind towards the PHP array pop function. But are you thinking about its effect on a multidimensional array? Well, as you already know that a multidimensional array is an array of arrays, every inner array in it is considered to be an element. Therefore, the array pop function will delete the last inner array completely. – Coding Example for PHP Array Pop With a Multidimensional ArrayIn an example case, you might have created a multidimensional array for storing the details of the members of a club. But then you came to know that the last-added member has withdrawn their membership. So, in this case, you’ll use the PHP array pop function to delete the complete inner array that contains the details of the member who left. Please feel free to create the above situation by using this code and try popping out the last inner array from a multidimensional array: <?php Here is the output that you’ll get: Array ( [0] => D-1 [1] => 20 [2] => GHI City ) Array ( [0] => Array ( [0] => A-1 [1] => 34 [2] => XYZ City ) [1] => Array ( [0] => C-3 [1] => 56 [2] => ABC City ) [2] => Array ( [0] => D-2 [1] => 39 [2] => GHI City ) [3] => Array ( [0] => B-4 [1] => 28 [2] => JKL City ) [4] => Array ( [0] => A-2 [1] => 27 [2] => ABC City ) ) Does It Return the Keys as Well?Although the array_pop in PHP works well with associative arrays too, you’ll notice a difference in the result returned by it. It is because the given function only returns the deleted value instead of the removed key-value pair. Therefore, you’ll not be able to save the key anywhere else after deleting it from the array. – Coding Example for PHP Array Pop With an Associative ArrayFor example, let’s say you have created an associative array and now, you want to pop out its last element. Here, the PHP array pop function will do the work with the only exception that it won’t return the deleted key. Please have a look at the code snippet corresponding to the example above and the output returned by it: <?php Here is the output of our code: smallArray ( [name] => cat [type] => carnivore [color] => white ) Benefit from Its ResultAs said in the beginning, you can use the result returned by the PHP array pop function later in your program. But where can you use it or how can you benefit from it? One of the basic examples that shows the advantages of the returned value is creating a trash bin functionality. The stated functionality allows storing all the deleted values. – Coding Example for Creating a Trash Bin Using PHP Array PopConsider this as an example: you have five equal-length arrays that contain employee information. Moreover, the arrays are created in such a way that one of the arrays contains employee ids, the other contains employee designations, and so on. It means that the corresponding values on a certain position in all the arrays are related to each other. Now, let’s say you want to delete the details of the last employee from all the arrays, but you don’t want the data to be permanently deleted. So, you’ll use the PHP array pop function multiple times for each employee array. Next, you’ll save the values returned by the PHP array pop and add them to another array. Please have a look at the below example code snippet for successfully adding the trash bin functionality to your program: <?php Here is the trash array that contains the deleted employee details: Array ( [0] => 5 [1] => Team Leader [2] => 2 Years [3] => May [4] => 2019 ) Remove Multiple Ending ElementsDoes the PHP array pop function come with limited functionality to remove only one last element? Although the answer to the above question is yes, there is a way out if you want to remove multiple ending elements. You can use a for loop along with the array_pop() function to delete more than one element from the end of an array. – Coding Example for Removing Multiple Ending ElementsFor instance, you might have an array of ages and now, you want to delete the last three elements from it. Therefore, you’ll define a variable with the quantity of the elements to be deleted. Next, you’ll create an empty array, start a for loop and call the pop.array() function inside it. The loop will run repeatedly and stop after deleting the required number of elements and storing them in another array. Here is a code snippet to help you out understand the example we illustrated above: <?php You’ll get this result on your browser when you run the coding block: Array ( [0] => 40 [1] => 38 [2] => 35 ) Array ( [0] => 10 [1] => 15 [2] => 20 [3] => 22 [4] => 24 [5] => 30 ) ConclusionFrom the discussion above, you understood well that the PHP array pop function efficiently helps in popping out the last elements of arrays. Also, here are some notable points you can return to when in doubt for the array_pop in PHP:
Position Is Everything: Your Go-To Resource for Learn & Build: CSS,JavaScript,HTML,PHP,C++ and MYSQL. What is difference between array push and pop in PHP?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. What is the opposite of array pop?array_pop() pops an element off end of the array. array_push() pushes an element into the end of the array. How can remove last key from array in PHP?The array_pop() function deletes the last element of an array. How can I change the last element of an array in PHP?Answer: Use the PHP array_pop() function You can use the PHP array_pop() function to remove an element or value from the end of an array. The array_pop() function also returns the last value of array. |