Home >Backend Development >PHP Tutorial >How to separate odd and even numbers in php array

How to separate odd and even numbers in php array

藏色散人
藏色散人Original
2019-01-14 13:21:464614browse

PHP How to separate odd and even elements from an array without using a loop?

How to separate odd and even numbers in php array

In PHP you will get an array of n elements. You have to separate the elements from the array based on whether the elements are odd or even. That is, print the odd and even arrays separately without looping over the original array or using any loops.

Example:

输入: array(2, 5, 6, 3, 0)
输出: 奇数array: 5 , 3
         偶数array: 2, 6, 0

输入: $input = array(0, 1, 2, 3, 4, 5)
输出: 奇数array: 1, 3, 5
         偶数array: 0, 2, 4

These types of problems can be easily solved by looping through the array and printing the odd number or even individual elements, but this will take up more lines of code and the code will Loop overhead occurs. So, to avoid using loops, we will try to use some inbuilt functions of PHP. Here we use two PHP array functions array_filter() and array_values() to solve this problem.

array_filter(): This function will be used to filter odd/even elements in the input array.

array_values(): This function will be used to re-index odd and even arrays because array_filter odd and even arrays have the same index that their elements have in the input array.

Note: The array_filter() function will only filter odd/even index elements and their index values. After applying the array_filter() function, the indexes of the odd array will be 1,3,5 and the indexes of the even array will be 0,2,4.

Algorithm:

Filter elements :

Filter odd elements through array_filter().

Filter even elements through array_filter().

Reindex arrays:

Reindex odd arrays using array_values().

Use array_values() to reindex even arrays.

Print odd/even array.

The following is the PHP implementation of the above algorithm:

<?php 
$input = array(4, 3, 6, 5, 8, 7, 2); 
function oddCmp($input) 
{ 
    return ($input & 1); 
} 
  
function evenCmp($input) 
{ 
    return !($input & 1); 
} 
  
$odd = array_filter($input, "oddCmp"); 
  
$even = array_filter($input, "evenCmp"); 
  
$odd = array_values(array_filter($odd)); 
  
$even = array_values(array_filter($even)); 
  
print"奇数数组:\n"; 
print_r($odd); 
  
print"偶数数组:\n"; 
print_r($even); 
  
?>

The output is as follows:

How to separate odd and even numbers in php array

##This This article is an introduction to the specific method of how to separate odd and even numbers in a php array (without using loops). I hope it will be helpful to friends in need!

The above is the detailed content of How to separate odd and even numbers in php array. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn