php delete array specific

PHPz
Release: 2023-05-07 17:08:11
Original
304 people have browsed it

Methods of elements

In the PHP development process, arrays are an integral part. Arrays can store large amounts of data, but sometimes we also need to delete specific elements from the array. So, now let us learn how to delete specific elements in PHP array.

1. Use the unset() function

There is a built-in function called unset() in PHP, which can delete one or more elements in the array. The syntax is as follows:

unset(array[key]);

Among them, array represents the array name of the element to be deleted, and key represents the key value of the element to be deleted.

The following is a sample code:

$fruits = array('apple', 'banana', 'cherry', 'orange');
unset($fruits[1] ); // Delete the "banana" element in the $fruits array
print_r($fruits);

The output result is:

Array
(

[0] => apple [2] => cherry [3] => orange
Copy after login

)

In the above example, we deleted the "banana" element in the $fruits array by using the unset() function.

However, in some cases, after we want to delete an element in the array, the subsequent elements will automatically fill this position. Fortunately, PHP has a built-in function that can solve this problem.

2. Use the array_splice() function

The array_splice() function can delete one or more elements in the array and re-index the remaining elements. The syntax is as follows:

array_splice(array,start,length);

Among them, array represents the array name of the element to be deleted, start represents the position from which to delete, and length represents the element to be deleted. The number of elements.

The following is a sample code:

$fruits = array('apple', 'banana', 'cherry', 'orange');
array_splice($fruits, 1, 1); // Delete the "banana" element in the $fruits array
print_r($fruits);

The output result is:

Array
(

[0] => apple [1] => cherry [2] => orange
Copy after login

)

In the above example, we deleted the "banana" element in the $fruits array by using the array_splice() function, and the subsequent elements automatically filled this position.

3. Use the array_diff() function

If we want to delete multiple specific elements in the array, we can use the array_diff() function. The array_diff() function returns the difference between two or more arrays. It returns elements in the original array that are not present in the comparison array. The syntax is as follows:

array_diff(array1,array2,array3...);

Among them, array1 represents the original array, array2, array3 and other expressions represent the arrays to be compared.

The following is a sample code:

$fruits = array('apple', 'banana', 'cherry', 'orange');
$remove = array('banana ', 'orange');
$result = array_diff($fruits, $remove); // Remove the "banana" and "orange" elements in the $fruits array
print_r($result);

The output result is:

Array
(

[0] => apple [1] => cherry
Copy after login

)

In the above example, we deleted the $fruits array by using the array_diff() function The "banana" and "orange" elements in .

4. Use foreach loop

The last way to delete array elements is to use foreach loop. It can iterate through each element in an array and remove specific elements based on conditions. Here is a sample code:

$fruits = array('apple', 'banana', 'cherry', 'orange');
foreach ($fruits as $key => $value) {
if ($value == 'banana' || $value == 'orange') {

unset($fruits[$key]); // 删除$fruits数组中的“banana”和“orange”元素
Copy after login

}
}
print_r($fruits);

The output result is:

Array
(

[0] => apple [2] => cherry
Copy after login

)

In the above example, we used a foreach loop to traverse each item in the $fruits array element. If the value of the current element is equal to "banana" or "orange", use the unset() function to delete them.

Conclusion

The above is the method of deleting specific elements of an array in PHP introduced in this article. According to different needs, you can choose a suitable method to operate the array. The usage and effect of each method have been explained in detail in the code examples. I hope it will be helpful to everyone.

The above is the detailed content of php delete array specific. 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!