How to find a string in an array in PHP?

前端 未结 6 435
孤独总比滥情好
孤独总比滥情好 2020-12-10 05:11

I have an array:

$array = array(\"apple\", \"banana\", \"cap\", \"dog\", etc..) up to 80 values.

and a string variable:

$st         


        
6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-10 05:33

    preg_match expects a string input not an array. If you use the method you described you will receive:

    Warning: preg_match() expects parameter 2 to be string, array given in LOCATION on line X

    You want in_array:

    if ( in_array ( $str , $array ) ) {
        echo 'It exists';
    } else {
        echo 'Does not exist';
    }
    

提交回复
热议问题