Change language

PHP Array_filter () function

|
Syntax:
array array_filter ($array, $callback_function, $flag)
Parameters : the function takes three parameters, of which one is required and the other two are optional.
  • $array(required): refers to the input array, for which the filtering operation should be performed.
  • $callback_function(optional): refers to a user-defined function. If the function is not specified, then all array entries that are FALSE will be removed.
  • $flag(optional): refers to the arguments passed to the callback function.
    • ARRAY_FILTER_USE_KEY - passes the key as the only argument to the callback function instead of the array value.
    • ARRAY_FILTER_USE_BOTH - passes the value and key as arguments to the callback instead of the value.
  • Return value : The function returns a filtered array. Below is a program showing how to return or filter even elements from an array using the array_filter() function.  
    // PHP function for checking even elements in an array function Even ( $array ) { // returns if the input integer is even if ( $array % 2 == 0) return TRUE; else return FALSE; } $array = array ( 12, 0, 0, 18, 27, 0, 46); print_r ( array_filter ( $array , "Even" ));  
    ?>
    Output:
    Array ([ 0] = > 12 [1] = > 0 [2] = > 0 [3] = > 18 [5] = > 0 [6] = > 46)
    B For this example, we will not pass a callback function and see the output. We will see that 0 or fake elements do not print:  
    // PHP function to check for even elements in an array function Even ( $array ) { // returns if the input is an integer even if ( $array % 2 == 0) return TRUE; else return FALSE; } $array = array ( 12, 0, 0, 18, 27, 0, 46); print_r ( array_filter ( $array ));  
    ?>
    Output:
    Array ([ 0] = > 12 [3] = > 18 [4] = > 27 [6] = > 46)
    Link : http://php.net/manual/en/function.array-filter.php

    Shop

    Learn programming in R: courses

    $

    Best Python online courses for 2022

    $

    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

    $

    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