How can you loop through an array in PHP to create a new array with specific criteria?

To loop through an array in PHP to create a new array with specific criteria, you can use a foreach loop to iterate over each element in the original array. Within the loop, you can add an if statement to check for the specific criteria and then add the element to the new array if the criteria are met. This allows you to filter or transform the elements of the original array based on your requirements.

<?php
// Original array
$originalArray = [1, 2, 3, 4, 5];

// New array with specific criteria (e.g., only even numbers)
$newArray = [];
foreach ($originalArray as $element) {
    if ($element % 2 == 0) {
        $newArray[] = $element;
    }
}

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