I needed this, and was happy enough with the results to share it.
This is like PHP in_array, but it searches for a substring.
for a one-dimensional array, it returns the key of the first match.
For a multi-dimensional array, it returns a csv list of keys to the first match.
<?php
function substr_in_array ($array, $term, $case=true )
{
//optional third argument set to false will cause case insensitive test.
// return false if the provide $array is not actually an array.
if(!is_array($array))
{
//echo 'Not Array';
return false;
}
// if $case has been set to false, make the term and each value lower case.
if(!$case)
{
$term=strtolower($term);
}
foreach ( $array as $key => $value )
{
if(is_array($value))
{
$inner=substr_in_array($value,$term,$case);
if ($inner!==false){return $key.','.$inner;}
}
else
{
if(!$case)
{
$value=strtolower($value);
}
echo 'Actually doing the text comparison now: is '.$term.' in '.$value.'?
'."\n";
// if the term is found in the value, return the $key.
if (strpos($value, $term ) !== false )
{
return $key;
}
}
}
return false;
}
?>
No comments:
Post a Comment