How to delete a piece of data in an array in php

PHPz
Release: 2023-04-21 10:36:47
Original
562 people have browsed it

In PHP development, we often encounter situations where arrays need to be modified. One common operation is to delete an element from an array. This article will introduce several methods to delete elements from PHP arrays.

Method 1: Use the unset() function

The unset() function in PHP can remove elements from the array. Its syntax format is as follows:

unset($array[index]);
Copy after login

Among them, $ Array is the name of the array where the element is to be deleted, and index is the index value of the element to be deleted.

For example, we have the following array:

$animals = array("dog", "cat", "lion", "tiger");
Copy after login
Copy after login
Copy after login

Now, we want to delete the second element "cat" in the array, we can use the following code:

unset($animals[1]);
Copy after login

After executing the above code, the elements in the $animals array will become:

Array ( [0] => dog [2] => lion [3] => tiger )
Copy after login
Copy after login

Method 2: Use the array_splice() function

The array_splice() function in PHP can also be used to delete the array Elements. The syntax format is as follows:

array_splice($array,start,length);
Copy after login

Among them, $array is the array name of the element to be deleted, start is the starting index value of the element to be deleted, and length is the number of elements to be deleted. If length is not set, all elements starting from start will be removed.

For example, we have the following array:

$animals = array("dog", "cat", "lion", "tiger");
Copy after login
Copy after login
Copy after login

Now, we want to delete the second element "cat" in the array, we can use the following code:

array_splice($animals,1,1);
Copy after login

After executing the above code, the elements in the $animals array will become:

Array ( [0] => dog [1] => lion [2] => tiger )
Copy after login

Method 3: Use the array_diff() function

The array_diff() function in PHP can be used to compare two arrays difference and returns a new array containing all elements in $array1 but not in $array2. If we pass the array itself as the first argument to the array_diff() function, we get a new array that does not contain the elements to be deleted.

For example, we have the following array:

$animals = array("dog", "cat", "lion", "tiger");
Copy after login
Copy after login
Copy after login

Now, we want to delete the second element "cat" in the array, we can use the following code:

$animals = array_diff($animals,array("cat"));
Copy after login

After executing the above code, the elements in the $animals array will become:

Array ( [0] => dog [2] => lion [3] => tiger )
Copy after login
Copy after login

Conclusion

The above three methods can delete elements in the PHP array. The method of using the unset() function and array_splice() function is more direct and more readable, but it will change the index value in the original array. The method using the array_diff() function will return a new array and will not change the index value in the original array, but it may be slightly slower.

In the actual development process, the most suitable method should be selected based on the actual situation.

The above is the detailed content of How to delete a piece of data in 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