Home  >  Article  >  Backend Development  >  How to delete specified elements in an array in php

How to delete specified elements in an array in php

PHPz
PHPzOriginal
2023-04-27 09:10:56795browse

In PHP programming, operating arrays is very common. Sometimes, we need to delete certain elements from an array, which requires the use of some functional functions provided by the development language.

This article will introduce how to use the functions provided by PHP to delete specified elements of the array.

  1. unset function

The unset function in PHP can be used to delete an element in an array. Using this function requires knowing the key value of the deleted element.

Example:

$fruits = array("apple", "banana", "orange", "grape");

unset($fruits[2]);

print_r($fruits);

In the above code, we created a fruit array and used the unset function to delete the element orange with index 2 in the array. Finally, we print the modified array.

The output result is:

Array

(

[0] => apple

[1] => banana

[3] => grape

)

After deletion, we can see that the element with index 2 has been deleted.

  1. array_splice function

In addition to using the unset function, we can also use PHP's array_splice function to delete elements at specified positions in the array. This function takes three parameters: the array of elements to delete, the position to start deletion from, and the number of elements to delete.

Example:

$fruits = array("apple", "banana", "orange", "grape");

array_splice($fruits, 2, 1);

print_r($fruits);

In the above code, we create an array of fruits and use the array_splice function to delete an element from the array. Among them, the first parameter represents the array of elements to be deleted, the second parameter represents the position to start deletion, and the third parameter represents the number of elements to be deleted.

Finally, we print the modified array.

The output result is:

Array

(

[0] => apple

[1] => banana

[3] => grape

)

You can see that the element orange with index 2 has been deleted.

  1. array_filter function

The array_filter function is used to filter elements in an array, and can filter elements according to specified conditions. If we want to filter out a specific element, we can use this function.

Example:

$fruits = array("apple", "banana", "orange", "grape");

$filtered_fruits = array_filter($fruits, function($value) {

return $value != "orange";

});

print_r($filtered_fruits);

In the above code, we create an array of fruits and use the array_filter function to delete an element. We define a callback function that will filter out all elements with a value of orange in the array based on the filter conditions in the function.

Finally, we print the modified array.

The output result is:

Array

(

[0] => apple

[1] => banana

[3] => grape

)

You can see that the element with the value orange has been removed from the array.

Summary

In PHP programming, deleting elements from an array is a common task. We can use unset, array_splice and array_filter functions to delete elements from an array. When choosing which function to use, you can make your choice based on where and why you need to remove elements.

The above is the detailed content of How to delete specified elements in an array in php. 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:How to define php arrayNext article:How to define php array