search a php array for partial string match

后端 未结 12 1269
南方客
南方客 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:42
    if(in_array("green",$arr)){
    //true 
    //do some code here
    }else{
    //false
    //ignore
    }
    
    0 讨论(0)
  • 2020-12-30 23:43

    This is a function for normal or multidimensional arrays.

    • Case in-sensitive
    • Works for normal arrays and multidimentional
    • Works when finding full or partial stings

    Here's the code (version 1):

    function array_find($needle, array $haystack, $column = null) {
    
        if(is_array($haystack[0]) === true) { // check for multidimentional array
    
            foreach (array_column($haystack, $column) as $key => $value) {
                if (strpos(strtolower($value), strtolower($needle)) !== false) {
                    return $key;
                }
            }
    
        } else {
            foreach ($haystack as $key => $value) { // for normal array
                if (strpos(strtolower($value), strtolower($needle)) !== false) {
                    return $key;
                }
            }
        }
        return false;
    } 
    

    Here is an example:

    $multiArray = array(
         0 => array(
                  'name' => 'kevin',
                  'hobbies' => 'Football / Cricket'),
          1 => array(
                  'name' => 'tom',
                  'hobbies' => 'tennis'),
           2 => array(
                  'name' => 'alex',
                  'hobbies' => 'Golf, Softball')
    );
    $singleArray = array(
            0 => 'Tennis',
            1 => 'Cricket',
    );
    
    echo "key is - ". array_find('cricket', $singleArray); // returns - key is - 1
    echo "key is - ". array_find('cricket', $multiArray, 'hobbies'); // returns - key is - 0 
    

    For multidimensional arrays only - $column relates to the name of the key inside each array. If the $needle appeared more than once, I suggest adding onto this to add each key to an array.

    Here is an example if you are expecting multiple matches (version 2):

    function array_find($needle, array $haystack, $column = null) {
    
        $keyArray = array();
    
        if(is_array($haystack[0]) === true) { // for multidimentional array
    
            foreach (array_column($haystack, $column) as $key => $value) {
                if (strpos(strtolower($value), strtolower($needle)) !== false) {
                    $keyArray[] = $key;
    
                }
            }
    
        } else {
            foreach ($haystack as $key => $value) { // for normal array
                if (strpos(strtolower($value), strtolower($needle)) !== false) {
                    $keyArray[] = $key;
                }
            }
        }
    
        if(empty($keyArray)) {
            return false;
        }
        if(count($keyArray) == 1) {
            return $keyArray[0];
        } else {
            return $keyArray;
        }
    
    }
    

    This returns the key if it has just one match, but if there are multiple matches for the $needle inside any of the $column's then it will return an array of the matching keys.

    Hope this helps :)

    0 讨论(0)
  • 2020-12-30 23:44

    PHP 5.3+

    array_walk($arr, function($item, $key) {
        if(strpos($item, 'green') !== false) {
            echo 'Found in: ' . $item . ', with key: ' . $key;
        }
    });
    
    0 讨论(0)
  • 2020-12-30 23:46

    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)
    
    0 讨论(0)
  • 2020-12-30 23:46

    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

    0 讨论(0)
  • 2020-12-30 23:47

    A quick search for a phrase in the entire array might be something like this:

    if (preg_match("/search/is", var_export($arr, true))) {
        // match
    }
    
    0 讨论(0)
提交回复
热议问题