<?php //RAY_filter_array.php
error_reporting(E_ALL);
echo "<pre>" . PHP_EOL;
function RAY_filter_array($array, $string)
{
// ITERATE OVER THE ARRAY TO REMOVE ELEMENTS THAT DO NOT HAVE THE REQUIRED STRING
foreach ($array as $key => $element)
{
// MAN PAGE: http://us.php.net/manual/en/function.strpos.php
if (strpos($element, $string) === FALSE) unset($array[$key]);
}
return $array;
}
// TEST DATA FOR THE FUNCTION
$arr = array
( 'foo'
, 'bar'
, 'baz'
)
;
// EXERCISE THE FUNCTION
var_dump(RAY_filter_array($arr, 'a'));
var_dump(RAY_filter_array($arr, 'ba'));
var_dump(RAY_filter_array($arr, 'baz'));
|