array_filter ( ) – Filter array elements using a callback function

The “array_filter()” function is used to filter elements of an array using a callback function.

Syntax:

    array_filter(array,function)

In the above syntax “array” is the array, each value of the array elements is passed to the user defined “function”, and the value returned “true” by the function is only displayed.

Example:

<?php
function callback_function($s)
{
if ($s==="baam")
{
return true;
}
return false;
}
$array=array(0=>"amit",1=>"baam",2=>"bikash");
$result = (array_filter($array,"callback_function"));
foreach($result as $key=>$value){
echo $key. ' : '. $value;
}
?>

Example:

1 : baam

Similar Query: