Cara menggunakan php delete last array

❮ PHP Array Reference

Example

Insert "blue" and "yellow" to the end of an array:

<?php
$a=array("red","green");
array_push($a,"blue","yellow");
print_r($a);
?>

Try it Yourself »


Definition and Usage

The 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).


Syntax

array_push(array, value1, value2, ...)

Parameter Values

ParameterDescription
array Required. Specifies an array
value1 Optional. Specifies the value to add (Required in PHP versions before 7.3)
value2 Optional. Specifies the value to add


Technical Details

Return Value:Returns the new number of elements in the array
PHP Version:4+
Change log:As of version 7.3 this function can be called with only the array parameter

More Examples

Example

An array with string keys:

<?php
$a=array("a"=>"red","b"=>"green");
array_push($a,"blue","yellow");
print_r($a);
?>

Try it Yourself »


❮ PHP Array Reference


PHP array_pop() Function

The PHP array_pop() function pops (deletes) the last element of the given array, and returns the value of popped item.

Table of Contents

  • PHP array_pop() Function
  • Syntax – array_pop()
  • Example 1: Pop the Last Element of Array
  • Example 2: Pop Elements of Array Iteratively in Loop
  • Returns Value
  • For example: Using string elements
  • What Is PHP Array Pop?
  • – Coding Example
  • Does It Remove the Whole Inner Array
  • – Coding Example for PHP Array Pop With a Multidimensional Array
  • Does It Return the Keys as Well?
  • – Coding Example for PHP Array Pop With an Associative Array
  • Benefit from Its Result
  • – Coding Example for Creating a Trash Bin Using PHP Array Pop
  • Remove Multiple Ending Elements
  • – Coding Example for Removing Multiple Ending Elements
  • What is difference between array push and pop in PHP?
  • What is the opposite of array pop?
  • How can remove last key from array in PHP?
  • How can I change the last element of an array in PHP?

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

ParameterDescription
array [mandatory] The array from which last element has to be popped.

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 Array

In 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 Loop

In 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

Conclusion

In 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 Value

The 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 elements

Output

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?
    • – Coding Example
    • – Note
  • Does It Remove the Whole Inner Array
    • – Coding Example for PHP Array Pop With a Multidimensional Array
  • Does It Return the Keys as Well?
    • – Coding Example for PHP Array Pop With an Associative Array
  • Benefit from Its Result
    • – Coding Example for Creating a Trash Bin Using PHP Array Pop
  • Remove Multiple Ending Elements
    • – Coding Example for Removing Multiple Ending Elements
  • Conclusion

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 Example

For 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
// creating an array of numbers
$num_arr = array(20,40,60,80,100);
// using the array.pop() function
$last = array_pop($num_arr);
// printing the removed element
echo “$last <br>”;
// printing the trimmed array
print_r($num_arr);
?>

You’ll get the following output on your browser when you run this code:

100

Array ( [0] => 20 [1] => 40 [2] => 60 [3] => 80 )

– Note

The 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 Array

Certainly, 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 Array

In 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
// creating a multidimensional array
$members = array(
array(“A-1”, 34 , “XYZ City”),
array(“C-3”, 56 , “ABC City”),
array(“D-2”, 39 , “GHI City”),
array(“B-4”, 28 , “JKL City”),
array(“A-2”, 27 , “ABC City”),
array(“D-1”, 20 , “GHI City”)
);
// using the array pop function
$get = array_pop($members);
// printing the removed inner array
print_r($get);
echo “<br>”;
// printing the trimmed multidimensional array
print_r($members);
?>

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 Array

For 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
// creating an associative array
$animal = array(
“name” => “cat”,
“type” => “carnivore”,
“color” => “white”,
“size” => “small”
);
// using the array pop function
$popped = array_pop($animal);
// printing the result returned by the array pop function
echo “$popped <br>”;
// printing the trimmed associative array
print_r($animal);
?>

Here is the output of our code:

smallArray ( [name] => cat [type] => carnivore [color] => white )

Benefit from Its Result

As 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 Pop

Consider 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
// creating multiple employee arrays
$emp_ids = array(1, 2, 3, 4, 5);
$emp_designations = array(“Accountant”,”IT Incharge”,”Manager”,”Executive”,”Team Leader”);
$emp_experience = array(“3 Years”, “4 Years”, “2 Years”, “5 Years”, “2 Years”);
$emp_joining_month = array(“July”, “June”, “August”, “March”, “May”);
$emp_joining_year = array(“2017”, “2018”, “2018”, “2019”, “2019”);
// using the array pop funtion
$id = array_pop($emp_ids);
$desig = array_pop($emp_designations);
$exp = array_pop($emp_experience);
$month = array_pop($emp_joining_month);
$year = array_pop($emp_joining_year);
// adding the returned values into the trash array
$trash = array($id, $desig, $exp, $month, $year);
// printing the trash array
print_r($trash);
?>

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 Elements

Does 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 Elements

For 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
// creating an array of ages
$ages = array(10, 15, 20, 22, 24, 30, 35, 38, 40);
// defining the number of elements that you want to delete
$count = 3;
// creating an array to store the popped elements
$popped = array();
// running the for loop
for($i = 0; $i < $count; $i++){
$popped[] = array_pop($ages);
}
// printing the popped array
print_r($popped);
echo “<br>”;
// printing the ages array
print_r($ages);
?>

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 )

Conclusion

From 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:

  • The PHP array pop function accepts an array and deletes its last element
  • The PHP array pop function returns the last deleted value of the array
  • A warning will be when you pass a non-array as an argument to the PHP array pop function
  • You will not get anything in the output when the passed array is empty
  • The array_pop function removes the last inner array entirely from a multidimensional array
  • You cannot get the key of the last deleted element by using the PHP array pop function
  • You can delete multiple ending elements by using the array_pop function inside a for loop

Certainly, now you can efficiently trim the arrays without losing your important data. Moreover, feel free to note down that you can create multiple trash arrays and add them to another array to create a deeper trash bin.
  • Author
  • Recent Posts

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.