Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search an array for a matching string

Tags:

arrays

string

php

I am looking for away to check if a string exists as an array value in an array is that possible and how would I do it with PHP?

like image 487
sea_1987 Avatar asked Jul 01 '26 06:07

sea_1987


1 Answers

If you simply want to know if it exists, use in_array(), e.g.:

$exists = in_array("needle", $haystack);

If you want to know its corresponding key, use array_search(), e.g.:

$key = array_search("needle", $haystack);
// will return key for found value, or FALSE if not found
like image 57
Rob Hruska Avatar answered Jul 03 '26 19:07

Rob Hruska