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

    Gifts for programmers

    Learn programming in R: courses

    $FREE
    Gifts for programmers

    Best Python online courses for 2022

    $FREE
    Gifts for programmers

    Best laptop for Fortnite

    $399+
    Gifts for programmers

    Best laptop for Excel

    $
    Gifts for programmers

    Best laptop for Solidworks

    $399+
    Gifts for programmers

    Best laptop for Roblox

    $399+
    Gifts for programmers

    Best computer for crypto mining

    $499+
    Gifts for programmers

    Best laptop for Sims 4

    $

    Latest questions

    PythonStackOverflow

    Common xlabel/ylabel for matplotlib subplots

    1947 answers

    PythonStackOverflow

    Check if one list is a subset of another in Python

    1173 answers

    PythonStackOverflow

    How to specify multiple return types using type-hints

    1002 answers

    PythonStackOverflow

    Printing words vertically in Python

    909 answers

    PythonStackOverflow

    Python Extract words from a given string

    798 answers

    PythonStackOverflow

    Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

    606 answers

    PythonStackOverflow

    Python os.path.join () method

    384 answers

    PythonStackOverflow

    Flake8: Ignore specific warning for entire file

    360 answers


    Wiki

    Python | How to copy data from one Excel sheet to another

    Common xlabel/ylabel for matplotlib subplots

    Check if one list is a subset of another in Python

    How to specify multiple return types using type-hints

    Printing words vertically in Python

    Python Extract words from a given string

    Cyclic redundancy check in Python

    Finding mean, median, mode in Python without libraries

    Python add suffix / add prefix to strings in a list

    Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

    Python - Move item to the end of the list

    Python - Print list vertically