Php Array Elements Filter Function

What is array_filter() Function?

Explanation

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 funct1($s)
{
if ($s==="Cherry")
{
return true;
}
return false;
}
$a=array(0=>"Orange",1=>"Cherry",2=>"Guava");
print_r(array_filter($a,"funct1"));
?>
Result :

Array ( [1] => Cherry )

In the above example each value of the array "$a" is compared using the "function", the value "Cherry" is displayed as the condition returns "True".

PHP Topics


Ask Questions

Ask Question