How can regular expressions be effectively used with preg_grep in PHP?

Regular expressions can be effectively used with preg_grep in PHP by providing a regular expression pattern as the first argument and an array of strings to search through as the second argument. This function will return an array containing the elements from the input array that match the regular expression pattern.

$pattern = '/^apple/';
$array = ['apple', 'banana', 'cherry', 'pineapple'];

$matches = preg_grep($pattern, $array);

print_r($matches);