Click to See Complete Forum and Search --> : Searching Arrays


nuthead
11-12-2003, 10:05 AM
I have an array which really contains 4 items per value eg.
array("a,b,c,d","b,c,d,e","c,d,e,f")
What i want to do is do a search for say "a" (to return the first entry) but when I try this with array_search() it doesnt return anything. I could do a search with regular expressions but would that return the whole array string ("a,b,c,d")?

Does that make sense? :confused:

pyro
11-12-2003, 11:58 AM
So, if you search for "a", you want it to return "a,b,c,d"?

nuthead
11-12-2003, 02:30 PM
yup, as "a,b,c,d" is 1 entry in an array. Also, if I searched for "b" it would bring back both "a,b,c,d" and "b,c,d,e" as both entries contain b.

pyro
11-12-2003, 03:08 PM
Try something like this:

<?PHP
$search = "b"; #letter you want to find
$array = array("a,b,c,d","b,c,d,e","c,d,e,f");
foreach ($array as $item) {
if (strstr($item, $search)) {
echo $item."<br />";
}
}
?>

nuthead
11-12-2003, 03:23 PM
I knew you would help pyro, it works a treat! :D

pyro
11-12-2003, 04:14 PM
Good deal... :)