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 :
- Filter elements :
- filter odd elements by array_filter().
- filter even elements by array_filter().
- Re-index Arrays :
- re-index odd-array by use of array_values().
- re-index even-array by use of array_values().
- Print both of the odd/even array.
Below is the PHP implementation of above algorithm:
<?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); ?> |
Output :
Odd array :
Array
(
[0] => 3
[1] => 5
[2] => 7
)
Even array :
Array
(
[0] => 4
[1] => 6
[2] => 8
[3] => 2
)
Recommended Posts:
- How to unpack array elements into separate variables using JavaScript ?
- How to loop through an associative array and get the key in PHP?
- How to use loop through an array in JavaScript?
- How to remove an array element in a foreach loop?
- Iterate associative array using foreach loop in PHP
- PHP | ImagickKernel separate() Function
- How to get the elements of one array which are not present in another array using JavaScript?
- JavaScript | Add new elements at the beginning of an array
- PHP | Remove duplicate elements from Array
- Find the OR and AND of Array elements using JavaScript
- How to iterate through all selected elements into an array ?
- Sum and Product of Array elements using JavaScript
- Remove elements from a JavaScript Array
- Reset keys of array elements using PHP ?
- Program to remove empty array elements in PHP
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.

