Change language

PHP Separate odd and even elements from an array without using a 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 
Problems of this type can be easily solved by traversing the array and printing elements that are odd and even separate, but that would require more lines of code, as well as the overhead of a loop in our code. So, to avoid using a loop, we will try to use some built-in PHP functions. Here we are using two PHP array functions array_filter() and array_values ​​() to solve this problem.
  • array_filter(): this the function will be used to filter odd / even elements from the input array.
  • array_values ​​(): this function will be used to reindex the odd and even array, since after array_filter the odd and even array have same index as their elements in the input array.
Note: The array_filter() function will only filter odd / even indexed elements along with the index value. After using the array_filter() function, the index of the odd array will be 1, 3, 5, and the index of the even array will be 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 the above algorithm:  
    // PHP program for splitting odd-even index
    // array elements 
    // input mass iv $input = array (4, 3, 6, 5, 8, 7, 2);  
    // comparator function for filtering odd elements function oddCmp ( $input ) { return ( $input & amp; 1 ); }  
    // comparator function for filtering odd elements function evenCmp ( $input ) { return ! ( $input & amp; 1); }  
    // filtering odd items $odd = array_filter ( $input , "oddCmp" );  
    // filter even index items $even = array_filter ( $input , "evenCmp" );  
    // re-index the odd array using array_values ​​() $odd = array_values ​​ ( array_filter ( $odd ));  
    // re-index the even array using array_values ​​() $even = array_values ​​ ( array_filter ( $even ));  
    // display odd-indexed array print "Odd array:" ; print_r ( $odd );  
    // display an even-indexed array print "Even array:" ; print_r ( $even );  
    ?>
    Output:
    Odd array: Array ([0] = > 3 [1] = > 5 [2] = > 7) Even array: Array ([0] = > 4 [1] = > 6 [2] = > 8 [3] = > 2) 

    PHP Separate odd and even elements from an array without using a loop PHP: Questions

    PHP Separate odd and even elements from an array without using a loop sep: Questions

    Shop

    Best laptop for Fortnite

    $

    Best laptop for Excel

    $

    Best laptop for Solidworks

    $

    Best laptop for Roblox

    $

    Best computer for crypto mining

    $

    Best laptop for Sims 4

    $

    Best laptop for Zoom

    $499

    Best laptop for Minecraft

    $590

    Latest questions

    NUMPYNUMPY

    psycopg2: insert multiple rows with one query

    12 answers

    NUMPYNUMPY

    How to convert Nonetype to int or string?

    12 answers

    NUMPYNUMPY

    How to specify multiple return types using type-hints

    12 answers

    NUMPYNUMPY

    Javascript Error: IPython is not defined in JupyterLab

    12 answers

    Wiki

    Python OpenCV | cv2.putText () method

    numpy.arctan2 () in Python

    Python | os.path.realpath () method

    Python OpenCV | cv2.circle () method

    Python OpenCV cv2.cvtColor () method

    Python - Move item to the end of the list

    time.perf_counter () function in Python

    Check if one list is a subset of another in Python

    Python os.path.join () method