Home  >  Article  >  Backend Development  >  php remove last one from array

php remove last one from array

WBOY
WBOYOriginal
2023-05-07 14:55:07466browse

In PHP, deleting the last element in an array is a common operation. This functionality can be achieved through built-in functions. In this article, we will discuss how to remove the last element of an array using PHP.

Arrays in PHP

In PHP, arrays are a composite data type. It can store multiple values. Array elements can be strings, numbers, objects, or other types of data.

There are three types of arrays in PHP: numeric, associative, and multidimensional arrays. Each element of a numeric array has a numeric index. Each element of an associative array has a string key. A multidimensional array is an array containing nested arrays.

The following is an example numeric array:

$numbers = array(1, 2, 3, 4, 5);

The following is an example associative array:

$fruits = array("apple" => "red", "banana" => "yellow", "orange" => "orange");

Delete the last element of the array

In PHP, delete There are two ways to get the last element of an array. We will discuss them below.

Using the array_pop() function

To use the PHP built-in function array_pop() to delete the last element in the array, you can follow the steps below:

1.Use The array_pop() function removes the last element from an array.

  1. If you want to save the deleted element to a variable, you can assign it to a variable.

Here is a sample code:

$numbers = array(1, 2, 3, 4, 5);
$last_element = array_pop($numbers);
print_r($numbers);
echo $last_element;

Run this code, the output will be:

Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )
5

As mentioned above, this code will remove from the array of numbers The last element and assigns it to the $last_element variable. Looking back at the printed array, you'll see that the last element has been removed.

Using the unset() function

To use PHP's unset() function to delete the last element of an array, you can follow these steps:

  1. Get the array The index of the last element.
  2. Use the unset() function to delete the element.

Here is a sample code:

$numbers = array(1, 2, 3, 4, 5);
$last_index = count($numbers)-1;
unset($numbers[$last_index]);
print_r($numbers);

Run this code, the output will be:

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

As mentioned above, this code will use the unset() function Removes the last element from an array of numbers. The advantage of this implementation is that there are no extra variables; the disadvantage is that you need to get the index in the original array that you want to delete.

Conclusion

You can easily delete the last element in an array using PHP's array_pop() function and unset() function. These built-in functions are quick and easy, with array_pop() being simpler and more intuitive, and unset() being more flexible and optional. In array operations, which method you choose depends on your preferences and needs.

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

Statement:
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
Previous article:php remove tag attributesNext article:php remove tag attributes