array_filter() : Filters elements by callback function

$new_array = array_filter ($array, call_back_function(),$flag);
ParameterDESCRIPTION
$arrayRequired : Input array to be filtered.
$call_back_function()Optional: Values or Keys to be passed for checking.
$flagOptional: 0: ARRAY_FILTER_USE_KEY , use only key for passing to callback function.
1 : ARRAY_FILTER_USE_BOTH : Use key and value both for passing to callback function.
We can use one callback function to check all the elements of the array. The callback function check each element and returns true or false. A new array is retuned with True value elements of the main array. Array key of parent array is preserved.

Filtering only integers.

We have one array with string and integer data. Let us try to filter elements and keep only integers.
<?Php
$a= array(1,2,'v','bcd',5,7.3,88,105 ); 
$b=array_filter($a, 'is_int');
print_r($b);
?>
The output is here
Array ( [0] => 1 [1] => 2 [4] => 5 [6] => 88 [7] => 105 )
In our new array $b all the integers take from old array $a is kept. Note that the associated keys of the elements are also retained. We used print_r to display our $b array. We have used is_int a built in PHP function which returns true if the element is an integer. Now let us try to use one user defined function as callback function.

Filtering based on length of the element

We will create a function check_length which returns True for the elements who are of 3 char length. We have used strlen() string function to get the length of the string.
<?Php
function check_length($str){
if(strlen($str) == 3 ){
return 'T';
}
}

$a= array(361,2,'vbc','bcd',5,7.3,88,10); 
$b=array_filter($a, 'check_length');
print_r($b);
?>
Output is here
Array ( [0] => 361 [2] => vbc [3] => bcd [5] => 7.3 )

Using Flag ( Optional )

Flag = 0 for ARRAY_FILTER_USE_KEY ( default )
Flag = 1 for ARRAY_FILTER_USE_BOTH

function check_size($value){
if(strlen($value) == 3 ){
return 'T';
}
}

function check_size_both($value,$key){
if(strlen($value) == 3 || is_int($key) ){
return 'T';
}
}

$input=array(1 =>'One',2=>'Two',3=>'Three','Four'=>'4th');
echo "-- With ARRAY_FILTER_USE_KEY --<br> ";
$result=array_filter($input,'check_size',0);
while (list ($key, $val) = each ($result)) {
echo "$key -> $val <br>";
}
echo "<br>-- With ARRAY_FILTER_USE_BOTH --<br> ";
$result=array_filter($input,'check_size_both',1);
while (list ($key, $val) = each ($result)) {
echo "$key -> $val <br>";
}
Output
-- With ARRAY_FILTER_USE_KEY --
1 -> One 
2 -> Two 
Four -> 4th 

-- With ARRAY_FILTER_USE_BOTH --
1 -> One 
2 -> Two 
3 -> Three 
Four -> 4th 
3 -> Three is included in result ( ARRAY_FILTER_USE_BOTH ) as the key is an integer though the length of the value is not equal to three.
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com




    Post your comments , suggestion , error , requirements etc here .




    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer