Page 1 of 1

search in array

Posted: Mon Oct 13, 2003 11:36 am
by yaron
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

Posted: Mon Oct 13, 2003 11:52 am
by murph
in_array ( )

Posted: Mon Oct 13, 2003 7:03 pm
by JAM
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.

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