Home >Backend Development >PHP Problem >How to delete elements with specified value from php array
PHP is a server-side scripting language. For PHP developers, arrays are a very important data structure. Arrays can store different types of data in one variable, which is very useful and convenient. In some cases, we need to delete a specific value from a PHP array, so how to do it?
PHP arrays provide a variety of methods to delete array elements. In this article, we will dive into the different ways on how to delete PHP array elements by value.
PHP’s built-in function unset() can be used to delete elements in a PHP array. This function deletes the corresponding value from the array through one or more specified array key names and releases its memory space.
Sample code:
$fruit = array('apple', 'banana', 'cherry', 'dates'); unset($fruit[1]); // 删除 $fruit 数组的第二个元素 print_r($fruit);
Output result:
Array ( [0] => apple [2] => cherry [3] => dates )
In the above sample code, we created an array named $fruit, which contains four elements: apple , banana, cherry and dates. We then removed the second element in the array (i.e. 'banana') using the unset() function and output the updated $fruit array via the print_r() function.
#PHP’s array_search() function is used to search for a specified value in an array and return the key name of the value . If we want to delete an element in the array, we can first use the array_search() function to find the key name of the element in the array, and then use the unset() function to delete the element corresponding to the key name.
Sample code:
$fruit = array('apple', 'banana', 'cherry', 'dates'); $key = array_search('banana', $fruit); unset($fruit[$key]); print_r($fruit);
Output result:
Array ( [0] => apple [2] => cherry [3] => dates )
In the above sample code, we used the array_search() function to find the 'banana' element in the array $fruit , and stores its corresponding key name in the variable $key. We then removed the 'banana' element from the array using the unset() function and output the updated $fruit array via the print_r() function.
PHP’s array_diff() function can be used to calculate the difference between two or more arrays. This function returns an array containing values that appear in the first array but not in any other array. The value of a specified element can be removed from the original array by comparing it to a new array containing only the elements that need to be retained.
Sample code:
$fruit = array('apple', 'banana', 'cherry', 'dates'); $delete_fruit = array('banana'); $result = array_diff($fruit, $delete_fruit); print_r($result);
Output result:
Array ( [0] => apple [2] => cherry [3] => dates )
In the above sample code, we created two arrays: $fruit and $delete_fruit. We then use the array_diff() function to delete the elements in $delete_fruit from the $fruit array and store the result in the variable $result. Finally, we use the print_r() function to output the updated $fruit array.
Conclusion
There are many ways to delete array elements in PHP, but the above three methods are the most commonly used and efficient methods:
Use the unset() function to directly delete elements in the array.
Use the array_search() function to find the key corresponding to the element in the array, and then use the unset() function to delete the element corresponding to the key.
Use the array_diff() function to form a new array with the elements that need to be deleted, compare it with the original array, and finally replace the original array with the obtained difference set.
The above three methods can help PHP developers quickly delete specified elements in the array.
The above is the detailed content of How to delete elements with specified value from php array. For more information, please follow other related articles on the PHP Chinese website!