What is the purpose of preg_grep in PHP and how is it commonly used?

The purpose of preg_grep in PHP is to search an array for elements that match a specific regular expression pattern. It is commonly used to filter an array based on a regular expression criteria.

$pattern = '/^a/';
$array = ['apple', 'banana', 'orange', 'grape'];
$filteredArray = preg_grep($pattern, $array);

print_r($filteredArray);