Hello all,
I want to perform a search through my array if a specific value is contained in one of the array elements.
php function search_array() is not helpful to me because it returns true only if the the values are the same.
I want to do a LIKE search meaning if there is a substring in the array element that matches my search!
I know I can loop the array myself and use substr.
is there a better way?
Thanks
search in array
Moderator: General Moderators
in_array() wont probably help if you are to use LIKE (as sql's like clause).
A foreach-loop looping the values combined with a regexp or strstr would do the trick.
A foreach-loop looping the values combined with a regexp or strstr would do the trick.
Code: Select all
<?php
// small example, using strstr
$array = array('cows', 'fooobars', 'EVILkittens');
$searchfor = 'ooo';
foreach ($array as $v) {
if (strstr($v, $searchfor)) {
echo 'Found: '.$v;
}
}
?>
Returns:
Found: fooobars