W3docs

How to Separate Odd and Even Elements from an Array Without Using a Loop in PHP

Here, we provide a snippet, explaining how to separate odd and even elements from an array without using a loop in PHP. Read on and check out the examples.

Suppose you have an array of n elements, and the task is to separate those elements based on whether they are even or odd. In other words, it is necessary to print an odd array and an even array without using an explicit loop.

Below, we will guide you in doing that.

Info

Please consider, first, trying your approach on {IDE} before getting to the solution.

Using array_filter() and array_values()

If you want to avoid using loops for carrying out the task mentioned above, then you need to use built-in PHP functions such as <kbd class="highlighted"> XFI2 </kbd> and <kbd class="highlighted"> XFI3 </kbd>.

Tip

Also, take into account that ** array_filter() ** filters elements based on their numeric value, not their index. After using ** array_filter() **, the resulting arrays will contain only odd or even numbers.

First, let’s see how the algorithm works:

  1. Filter elements :
    • filter odd elements by <kbd class="highlighted"> XFI4 </kbd>.
    • filter even elements by <kbd class="highlighted"> XFI5 </kbd>.
  2. Re-index Arrays :
    • re-index odd-array by use of <kbd class="highlighted"> XFI6 </kbd>.
    • re-index even-array by use of <kbd class="highlighted"> XFI7 </kbd>.
  3. Print both the odd and even arrays.

The illustration of the algorithm above is as follows:

php functions to separate odd and even values of an array

<?php

// PHP program to separate odd-even indexed
// elements of an array

// input array
$input = [4, 3, 6, 5, 8, 7, 2];

// comparator function to filter odd elements
function oddCmp($input)
{
    return $input & 1;
}

// comparator function to filter even elements
function evenCmp($input)
{
    return !($input & 1);
}

// filter odd elements
$odd = array_filter($input, "oddCmp");

// filter even elements
$even = array_filter($input, "evenCmp");

// re-index odd array by use of array_values()
$odd = array_values($odd);

// re-index even array by use of array_values()
$even = array_values($even);

// print odd-valued array
print "Odd array :\n";
var_dump($odd);

// print even-valued array
print "\nEven array :\n";
var_dump($even);

php functions to separate odd and even values of an array

Odd array :
Array
(
    [0] => 3
    [1] => 5
    [2] => 7
)

Even array :
Array
(
    [0] => 4
    [1] => 6
    [2] => 8
    [3] => 2
)

Describing the array_filter() Function

This function iterates over every value in the array and passes them to the callback function.

Once the callback function returns true, the current value from the array is included in the result array.

The <kbd class="highlighted"> XFI8 </kbd> function has the following parameters: <kbd class="highlighted"> XFI9 </kbd>, <kbd class="highlighted"> XFI10 </kbd>, and <kbd class="highlighted"> XFI11 </kbd>.

The first one is the <kbd class="highlighted"> XFI12 </kbd> to iterate over. The callback is the <kbd class="highlighted"> XFI13 </kbd> function to apply. And, the <kbd class="highlighted"> XFI14 </kbd> specifies what arguments are forwarded to the callback.

Describing the array_values() Function

This function is used for returning all the values from the array and indexing the array numerically. It is mainly used when the original keys are no longer needed.