Filtering Arrays Using array_filter() : array_filter « Data Structure « PHP






Filtering Arrays Using array_filter()

 
<?php 
function array_display($array, $pre=FALSE) { 
    $tag = $pre ? 'pre' : 'p'; 
    printf("<%s>%s</%s>\n", $tag, var_export($array, TRUE), $tag); 
} 

function is_romance($lang) { 
    return in_array($lang, array('French', 'Spanish', 'Portuguese', 'Italian')); 
} 


$countries = array( 'USA' => 'English', 'Spain' => 'Spanish', 'Brazil' => 'Portuguese'); 
$rom_countries = array_filter($countries, 'is_romance'); 
array_display($rom_countries, TRUE); 
?>
  
  








Related examples in the same category

1.Array filter
2.Using the array_filter() Function
3.array_filter demo
4.array_filter( ) filters elements through a function