PHP | Separate odd and even elements from array without using loop Last Updated : 08 Mar, 2018 Comments Improve Suggest changes Like Article Like Report 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 ) Comment More infoAdvertise with us Next Article PHP | Separate odd and even elements from array without using loop S Shivam.Pradhan Follow Improve Article Tags : Web Technologies PHP PHP-basics Similar Reads Remove First Element from an Array in PHP Given an array, the task is to remove the first element from an array in PHP. Examples:Input: arr = [1, 2, 3, 4, 5, 6, 7]; Output: 2, 3, 4, 5, 6, 7 Input: arr = [3, 4, 5, 6, 7, 1, 2] Output: 4, 5, 6, 7, 1, 2Below are the methods to remove the first element from an array in PHP:Table of ContentUsing 3 min read Removing Array Element and Re-Indexing in PHP In order to remove an element from an array, we can use unset() function which removes the element from an array, and then use array_values() function which indexes the array numerically automatically. Function Usedunset(): This function unsets a given variable. Syntax:void unset ( mixed $var [, mix 2 min read PHP | Remove duplicate elements from Array You are given an Array of n-elements.You have to remove the duplicate values without using any loop in PHP and print the array. Examples: Input : array[] = {2, 3, 1, 6, 1, 6, 2, 3} Output : array ( [6] => 2 [7] => 3 [4] => 1 [5] => 6 ) Input : array[] = {4, 2, 7, 3, 2, 7, 3} Output : arr 3 min read PHP Program to Find the Number Occurring Odd Number of Times Write a PHP program for a given array of positive integers. All numbers occur an even number of times except one number which occurs an odd number of times. Find the number in O(n) time & constant space. Examples : Input: arr = {1, 2, 3, 2, 3, 1, 3}Output : 3 Input: arr = {5, 7, 2, 7, 5, 2, 5}Ou 3 min read JavaScript program to print even numbers in an array Given an array of numbers and the task is to write a JavaScript program to print all even numbers in that array. We will use the following methods to find even numbers in an array: Table of Content Method 1: Using for Loop Method 2: Using while Loop Method 3: Using forEach LoopMethod 4: Using filter 5 min read How to Slice an Array in PHP? In PHP, slicing an array means taking a subset of the array and extracting it according to designated indices. When you need to extract a subset of elements from an array without changing the original array, this operation comes in handy. PHP comes with a built-in function called array_slice() to he 2 min read How to select all even/odd rows in table using jQuery ? In this article, we will see how to make a table by selecting the alternate rows i.e. selecting the even or odd rows by clicking on the respective buttons. This feature can be useful at the time of selecting the specific data/elements of either of the rows or to highlight the table of data for displ 3 min read Print all Odd Numbers in a Range in JavaScript Array Odd numbers are numbers that cannot be divided into equal parts. If we divide the odd number by 2, the remainder is 1. In JavaScript, if we want to print all Odd numbers in a range, we can print it by iterating over the array, applying the condition, and printing the odd numbers. There are various a 4 min read Print all Even Numbers in a Range in JavaScript Array We have to find all even numbers within a given range. To solve this question we are given the range(start, end) in which we have to find the answer. There are several ways to print all the even numbers in a range in an array using JavaScript which are as follows: Table of Content Using for Loop in 3 min read How to Filter a Collection in Laravel? In Laravel, filtering a collection involves selecting specific items based on set conditions, offering flexibility and control over data. This process helps refine collections to meet specific criteria for enhanced data management. Below are the methods to filter a collection in Laravel: Table of Co 3 min read Like