PHP | Separate odd and even elements from array without using loop

You are given an array of n elements in PHP. You have to separate the elements from the array based on the elements are odd or even. That is, print odd array and even array separately without traversing the original array or using any loop.

Examples:

Input : array(2, 5, 6, 3, 0)
Output : Odd array: 5 , 3
         Even array: 2, 6, 0

Input : $input = array(0, 1, 2, 3, 4, 5)
Output : Odd array: 1, 3, 5
         Even array: 0, 2, 4



These type of problems can be easily solved by traversing the array and printing the elements which are odd and even separately, but that will take more lines of code and also a overhead of loop in our code. So, to avoid use of loop we will try to use some inbuilt functions of PHP. Here we are using two PHP array functions array_filter() and array_values() to solve this problem.

  • array_filter() : This function will be used to filter the odd/even elements from the input array.
  • array_values() : This function will be used to re-index odd and even array as after array_filter odd and even array have same index as their elements have in input array.

Note: The array_filter() function will only filter odd/even indexed element along with their index value. After applying array_filter() function, index of odd-array will be 1, 3, 5 and that of even-array will be like 0, 2, 4.

Algorithm :

  1. Filter elements :
    • filter odd elements by array_filter().
    • filter even elements by array_filter().
  2. Re-index Arrays :
    • re-index odd-array by use of array_values().
    • re-index even-array by use of array_values().
  3. Print both of the odd/even array.

Below is the PHP implementation of above algorithm:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// PHP program to separate odd-even indexed
// elements of an array
  
// input array 
$input = array(4, 3, 6, 5, 8, 7, 2);
  
// comparator function to filter odd elements
function oddCmp($input)
{
    return ($input & 1);
}
  
// comparator function to filter odd elements
function evenCmp($input)
{
    return !($input & 1);
}
  
// filter odd-index elements
$odd = array_filter($input, "oddCmp");
  
// filter even-index elements
$even = array_filter($input, "evenCmp");
  
// re-index odd array by use of array_values()
$odd = array_values(array_filter($odd));
  
// re-index even array by use of array_values()
$even = array_values(array_filter($even));
  
// print odd-indexed array
print"Odd array :\n";
print_r($odd);
  
// print even-indexed array
print"\nEven array :\n";
print_r($even);
  
?>

chevron_right


Output :

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

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


My Personal Notes arrow_drop_up

Discovering ways to develop a plane for soaring career goals

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.