Change language

PHP Array_reduce () function

|
Syntax :
array_reduce ($array, own_function, $initial)
Parameters:
Function takes three arguments and is described below:
  • $array (required):this is a required parameter and refers to the original array that we must reduce from.
  • own_function (required):this parameter is also required and refers to a custom function used to store the value of the $array
  • $initial (optional): This parameter is optional and refers to the value that will be sent to the function.
  • Return Value:This function returns a minified result. It can be any int, float or string.Examples:
    Input: $array = (15, 120, 45, 78) $initial = 25 own_function() takes two parameters and concatenates them with "and" as a separator in between Output: 25 and 15 and 120 and 45 and 78 Input: $array = array (2, 4, 5); $initial = 1 own_function() takes two parameters and multiplies them. Output: 40
    In this program we will see how an array of integer elements is reduced to a single string value. We also passed the first element of our selection.
    // PHP function to illustrate the use of array_reduce() function own_function ( $element1 , $element2 ) { return $element1 . "and" . $element2 ; } $array = array ( 15, 120, 45, 78); print_r ( array_reduce ( $array , "own_function" , "Initial" )); ?> Output:
    Initial and 15 and 120 and 45 and 78
    In the program below, array_reduce reduces the given array to the product of all array elements using own_function().
    // PHP function to illustrate the use of array_reduce() function own_function ( $element1 , $element2 ) { $element1 = $element1 * $element2 ; return $element1 ; } $array = array ( 2, 4, 5, 10, 100); print_r ( array_reduce ( $array , "own_function" , "2" )); ?> Output:
    80000  Link : 
    http://php.net/ manual / en / function.array-reduce.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