CopyPastehas never been so tasty!

Filter Arrays Using array_filter() in PHP

by anonymous

  • 0
  • 0
  • 0

array_filter() function:

The array_filter() function passes each value/element of an array to user defined function, and return true or false and return any array value only that is true.

array_filter(array,user_defined_function) the function take two parameters and both are required.

First is an array and second is user defined function.


1. <?php
2. function user_Defined($array)
3. {
4. if ($array==="Rizwan")
5. {
6. return true;
7. }
8. return false;
9. }
10. $names=array("Tahreem","Rizwan","Hamid","Dali","Farz");
11. print_r(array_filter($names,"user_Defined"));
12. ?>


Array ( [1] => Rizwan )


On line 10 we have declared $names array and initialize it. On line 11 we have passed it to array_filter function of php. The array_filter function of php has two parameters first is array and second is user defined function. That passes through an array and returns the array value only that is true in user defined function.

Add A Comment: