Skip to content
PHP Logo

What are some alternative approaches to achieving the same result without using SPL in PHP?

Using SPL in PHP can sometimes be complex or overkill for certain tasks. One alternative approach to achieving similar results without using SPL is to manually iterate over arrays using foreach loops or array functions like array_map, array_filter, and array_reduce.

php
// Example: Manually iterating over an array to filter out even numbers
$numbers = [1, 2, 3, 4, 5, 6];
$filteredNumbers = [];
foreach ($numbers as $number) {
    if ($number % 2 !== 0) {
        $filteredNumbers[] = $number;
    }
}
print_r($filteredNumbers);