Php array_fill() & array_filter() Function

the funcxtion simply fills up an array with the contents you specify with the following syntax:

aray_fill(start,number,value)

Legend:
start – a numeric value which denotes the starting index of the key (required)
number – also a numeric value which denotes the number of entries (required)
value – denotes the value which is to be inserted into the array (required)

Sample code:

$a=array_fill(3,2,"Fly");
print_r($a);
?>

Output: Array ( [3] => Fly [4] => Fly )

PHP array_filter() Function
This function on the other hand checks each value of the array against a function you define and returns a true or false, returning only the ones that returned true values. The function is used with the following Syntax:

array_filter(array,function)

Legend :
array – denotes an array
function – user defined function that is to be satisfied

Sample code:
function urfunction(&v)
{
if ($v==="Fish")
{
return true;
}
return false;
}
$a=array(0=>“Bird”,1=>”Fish”,2=>”Mouse”);
print_r(array_filter($a,”myfunction”));
?>

Result:
Array ( [1] => Fish )

Comments are closed.