Using a loop to check the index of the element in the array is even or odd to get every other element. Examples of getting even or odd members of an array, using foreach loop and "modulo operation".In mathematics, the modulo operation is the remainder when one number is divided by another. It is denoted using the symbol % (percent sign) in many programming languages, including PHP.
Source code viewer
// Even members of an array: foreach ($a as $nr) { if (!($nr % 2)) { echo $nr . ','; } } // Output: 0,2,4,6, // Odd members of an array: foreach ($a as $nr) { if ($nr % 2) { echo $nr . ','; } } // Output: 1,3,5,Programming Language: PHP