What is the best approach to remove specific parts of string elements in an array in PHP?

When wanting to remove specific parts of string elements in an array in PHP, one approach is to use the array_map() function along with a custom callback function that utilizes the str_replace() function to remove the desired parts of the strings. This allows you to iterate over each element in the array and modify them accordingly.

<?php
// Original array with strings
$array = ["apple pie", "banana split", "cherry cheesecake"];

// Function to remove "pie" from each string
function removePie($str) {
    return str_replace("pie", "", $str);
}

// Apply the removePie function to each element in the array
$newArray = array_map("removePie", $array);

// Output the modified array
print_r($newArray);
?>