What are the potential drawbacks of using array_filter() to filter lines in PHP?

One potential drawback of using array_filter() to filter lines in PHP is that it can be inefficient for large arrays as it iterates over each element to apply the filtering function. To solve this issue, you can use a more efficient method such as using a foreach loop to filter the lines directly without the overhead of array_filter().

// Sample array of lines
$lines = ["line 1", "line 2", "line 3"];

// Filter lines containing "line 2" using array_filter
$filteredLines = array_filter($lines, function($line) {
    return strpos($line, "line 2") !== false;
});

// Use foreach loop to filter lines directly
$filteredLines = [];
foreach ($lines as $line) {
    if (strpos($line, "line 2") !== false) {
        $filteredLines[] = $line;
    }
}

print_r($filteredLines);