Home > Backend Development > PHP Problem > How to remove the last number from an array in php

How to remove the last number from an array in php

PHPz
Release: 2023-04-23 15:07:24
Original
667 people have browsed it

Removing the last number from an array in PHP is a very common problem. Sometimes we need to do this to avoid unnecessary problems. This article will introduce several methods to remove the last number of an array in PHP.

Method 1: array_pop function

The array_pop function can remove the last element in the array and return the removed element. Here is an example:

$array = array(1, 2, 3, 4, 5);
$last_element = array_pop($array);
print_r($array);
Copy after login

Output:

Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )
Copy after login
Copy after login
Copy after login

In the above example, we have defined an array with 5 elements. Then we call the array_pop function, which returns the last element in the array and removes it from the array. Finally, we print out the removed array.

Method 2: array_slice function

Another function that can be used to remove the last element of an array is array_slice. This function returns a new array containing a portion of the array. Here is an example:

$array = array(1, 2, 3, 4, 5);
$new_array = array_slice($array, 0, -1);
print_r($new_array);
Copy after login

Output:

Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )
Copy after login
Copy after login
Copy after login

In the above example, we use the array_slice function to copy all the elements from the original array into a new array. This new array is all the elements in the original array except the last element. We pass two parameters to the array_slice function, the first parameter is the original array, the second parameter is the starting index to be copied, and the last parameter is the ending index to be copied. Here we use -1, which means that the end index is the second to last element in the array.

Method three: unset function

The last method is to use the unset function to delete the last element in the array. This function directly changes the original array. Below is the sample code:

$array = array(1, 2, 3, 4, 5);
unset($array[count($array)-1]);
print_r($array);
Copy after login

Output:

Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )
Copy after login
Copy after login
Copy after login

In the above example, we have removed the last element using the unset function. We do this by calculating the length of the array and then deleting the element whose index is the array length minus 1.

Summary

These three methods can be used to remove the last element from the array. Which method to use depends on your preference and specific needs. If you just need to delete the last element and don't need to generate a new array, then using unset is the easiest way. If you need a new array, it's better to use array_slice.

When writing code, we should give priority to built-in functions because they have been tested and are much faster than functions written by ourselves.

The above is the detailed content of How to remove the last number from an array in php. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template