Wednesday, 24 September 2014

array_filter in PHP

array_filter() is used to generate the subset of input array analyzing their values according to the function called.
PHP array_filter() function returned array contains only those values, which function returns true.

Syntax: array_filter(array,function)
Parameters Description:
array : Required. Defines an array.

function : Required. Name of the user-made function.

Example:

<?php
function myfunc($par1)
{
if($par1 === "Sunday")
{
return  true;
}
return false;
}
$days = array( 0=>"Sunday", 1=>"Monday", 2=>"Tuesday" );
print_r( array_filter($days,"myfunc") );
?>

O/P:


Array (
          [0] => Sunday
        )

0 comments:

Post a Comment