How does php get rid of the fetched values in the array?

04-01-2024

In PHP, array is a very common data structure. In application, we often need to traverse the elements in the array, and we need to dynamically delete the elements that have been taken out during the traversal to ensure that the subsequent traversal operations can be carried out normally. This article will introduce how to remove the fetched values from the array in PHP.

First, the basic method of deleting elements from an array

PHP provides a variety of ways to delete elements in an array. Generally speaking, there are two ways:

Use array functions: PHP provides several functions to handle arrays, such as array _ shift (), array _ pop (), unset (), array _ splice (), etc. These functions can all be used to delete elements in arrays. Use loop traversal: Another way is to use loop traversal to find the element to be deleted and use unset () function to delete it. Let's first introduce the way to use array functions.

The array_shift () function array_shift () function removes the first element in the array and returns the value of that element, and the keys of other elements are reordered. The sample code is as follows:

$fruits = array("apple", "banana", "orange"); $firstFruit = array_shift($fruits); echo $firstFruit; //Output: apple

This function is very suitable for deleting the first element in an array and using the value of that element after deletion.

The array_pop () function array_pop () function removes the last element in the array and returns the value of that element. The sample code is as follows:

$fruits = array("apple", "banana", "orange"); $lastFruit = array_pop($fruits); echo $lastFruit; //output: orange

This function is very suitable for deleting the last element in an array and using the value of that element after deletion.

Unset () function unset () function is used to delete the specified elements in the array. The sample code is as follows:

$fruits = array("apple", "banana", "orange"); unset($fruits[1]); //Delete the element bananaprint_r($fruits) with subscript 1 in the array; //output: Array ([0] => apple [2] => orange)

This function is very suitable for deleting an element with a specified subscript, and there is no need to access the value of the element after deletion.

Array_splice () function The array_splice () function is used to delete the elements specified in the array and can replace the deleted elements with new ones. The sample code is as follows:

$fruits = array("apple", "banana", "orange"); array_splice($fruits, 1, 1); //Delete the element bananaprint_r($fruits) with subscript 1 from the array; //output: Array ([0] => apple [1] => orange)

This function is very suitable for deleting an element with a specified subscript and replacing it with a new element after deletion.

Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us