Search an array for a matching string

前端 未结 7 1305
渐次进展
渐次进展 2021-01-23 01:26

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?

7条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-23 02:14

    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
    

提交回复
热议问题