Getting every other member of array in PHP. Examples of getting even or odd members of an array, using foreach loop and "modulo operation". The modulo operation (division by %) finds the remainder after division of one number by another.
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