search a php array for partial string match

后端 未结 12 1439
南方客
南方客 2020-12-30 22:47

I have an array and I\'d like to search for the string \'green\'. So in this case it should return the $arr[2]

$arr = array(0 =>         


        
12条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-30 23:36

    You can use preg_grep function of php. It's supported in PHP >= 4.0.5.

    $array = array(0 => 'blue', 1 => 'red', 2 => 'green string', 3 => 'red');
    $m_array = preg_grep('/^green\s.*/', $array);
    

    $m_array contains matched elements of array.

提交回复
热议问题