What is the difference between using preg_grep and strpos for searching in PHP arrays?

When searching in PHP arrays, preg_grep is used to perform a regular expression match on array values, while strpos is used to find the position of the first occurrence of a substring in a string. If you need to search for a specific pattern or match in array values, preg_grep is more suitable. If you are looking for a simple substring match, strpos is the better option.

// Using preg_grep to search for a specific pattern in array values
$array = ["apple", "banana", "cherry"];
$pattern = "/^b/";
$matches = preg_grep($pattern, $array);
print_r($matches);

// Using strpos to search for a substring in array values
$array = ["apple", "banana", "cherry"];
$substring = "ba";
foreach ($array as $value) {
    if (strpos($value, $substring) !== false) {
        echo $value . " contains 'ba'\n";
    }
}