search a php array for partial string match

84,770

Solution 1

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.

Solution 2

For a partial match you can iterate the array and use a string search function like strpos().

function array_search_partial($arr, $keyword) {
    foreach($arr as $index => $string) {
        if (strpos($string, $keyword) !== FALSE)
            return $index;
    }
}

For an exact match, use in_array()

in_array('green', $arr)

Solution 3

There are several ways...

$arr = array(0 => 'blue', 1 => 'red', 2 => 'green string', 3 => 'red');

Search the array with a loop:

$results = array();

foreach ($arr as $value) {

  if (strpos($value, 'green') !== false) { $results[] = $value; }

}

if( empty($results) ) { echo 'No matches found.'; }
else { echo "'green' was found in: " . implode('; ', $results); }

Use array_filter():

$results = array_filter($arr, function($value) {
    return strpos($value, 'green') !== false;
});

In order to use Closures with other arguments there is the use-keyword. So you can abstract it and wrap it into a function:

function find_string_in_array ($arr, $string) {

    return array_filter($arr, function($value) use ($string) {
        return strpos($value, $string) !== false;
    });

}

$results = find_string_in_array ($arr, 'green');

if( empty($results) ) { echo 'No matches found.'; }
else { echo "'green' was found in: " . implode('; ', $results); }

Here's a working example: http://codepad.viper-7.com/xZtnN7

Solution 4

PHP 5.3+

array_walk($arr, function($item, $key) {
    if(strpos($item, 'green') !== false) {
        echo 'Found in: ' . $item . ', with key: ' . $key;
    }
});

Solution 5

for search with like as sql with '%needle%' you can try with

$input = preg_quote('gree', '~'); // don't forget to quote input string!
$data = array(
    1 => 'orange',
    2 => 'green string',
    3 => 'green', 
    4 => 'red', 
    5 => 'black'
    );
$result = preg_filter('~' . $input . '~', null, $data);

and result is

{
  "2": " string",
  "3": ""
}
Share:
84,770
ralixyle
Author by

ralixyle

Updated on July 09, 2020

Comments

  • ralixyle
    ralixyle almost 4 years

    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 => 'blue', 1 => 'red', 2 => 'green string', 3 => 'red');
    

    is there any predefined function like in_array() that does the job rather than looping through it and compare each values?

  • Vlad Preda
    Vlad Preda about 11 years
    Don't forget the proper usage of strpos. if (strpos($string, 'green') !== FALSE)
  • MrCode
    MrCode about 11 years
    This won't work. strpos() never returns true, it only returns either an integer or false.
  • Usman Ahmed
    Usman Ahmed almost 4 years
    The person asked for a partial match. Not the exact match.
  • Dennisrec
    Dennisrec over 2 years
    The question clearly says looping is not an option cuz that would be too obvious
  • Dennisrec
    Dennisrec over 2 years
    The question clearly says looping is not an option.
  • Dennisrec
    Dennisrec over 2 years
    The question clearly says looping is not an option.
  • Dennisrec
    Dennisrec over 2 years
    The question clearly says looping is not an option.