How to use array_filter() Function In PHP
The array_filter() function filters elements of an array using a callback function.
Example: The array_filter() function uses a callback to filter out elements that do not match the condition.
<?php
$arr = array(1, 2, 3, 4, 5);
// Remove the element with value 3
$arr = array_filter($arr, function($value) {
return $value != 3;
});
print_r($arr);
?>
Output
Array ( [0] => 1 [1] => 2 [3] => 4 [4] => 5 )
Different Ways to Delete an Item From an Array in PHP
Given an array containing some elements, the task is to Delete an item from an array using PHP.
Below are the approaches to delete an item from an Array in PHP:
Table of Content
- Using unset() Function
- Using array_splice() Function
- Using array_diff() Function
- Using array_filter() Function
- Using array_search() and unset() Functions
- Using a Loop to Remove Multiple Occurrences
Contact Us