Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • PHP Tutorial
  • PHP Exercises
  • PHP Array
  • PHP String
  • PHP Calendar
  • PHP Filesystem
  • PHP Math
  • PHP Programs
  • PHP Array Programs
  • PHP String Programs
  • PHP Interview Questions
  • PHP GMP
  • PHP IntlChar
  • PHP Image Processing
  • PHP DsSet
  • PHP DsMap
  • PHP Formatter
  • Web Technology
Open In App
Next Article:
PHP | Ds\Map reduce() Function
Next article icon

PHP | Ds\Map reduce() Function

Last Updated : 21 Aug, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
The Ds\Map::reduce() function is an inbuilt function in PHP which is used to reduce the map to a single value by applying operations using the callback function. Syntax:
mixed public Ds\Map::reduce( $callback, $initial )
Parameter: This function accepts two parameters as mentioned above and described below:
  • $callback: This parameter holds the function which contains the operation on the elements and store carry. This callback function contains three arguments which are listed below:
    • carry: It holds the return value of the previous callback, or initial if it's the first iteration.
    • key: It holds the key of the current iteration.
    • value: It holds the value of the current iteration.
  • $initial: This parameter holds the initial value of carry, which can be NULL.
Return value: This function returns the final value returned by the callback function. Below programs illustrate the Ds\Map::reduce() function in PHP: Program 1: php
<?php   // Declare a new map $map = new \Ds\Map(["a" => 1, "b" => 3, "c" => 5]);    echo("Map Elements\n");    print_r($map);    // Callback function with reduce function  echo("\nElement after performing operation\n");   var_dump($map->reduce(function($carry, $key, $element) {      return $carry + $element + 2;  }));    ?>  
Output:
  Map Elements  Ds\Map Object  (      [0] => Ds\Pair Object          (              [key] => a              [value] => 1          )        [1] => Ds\Pair Object          (              [key] => b              [value] => 3          )        [2] => Ds\Pair Object          (              [key] => c              [value] => 5          )    )    Element after performing operation  int(15)  
Program 2: php
<?php    // Declare new Map elements $map = new \Ds\Map(["a" => 10, "b" => 20,             "c" => 30, "d" => 40, "e" => 50]);    echo("Original map elements\n");    print_r($map);    $func_gfg = function($carry, $key, $element) {      return $carry * $element;  };    echo("\nMap after reducing to single element\n");    // Use reduce() function  var_dump($map->reduce($func_gfg, 10));    ?>  
Output:
  Original map elements  Ds\Map Object  (      [0] => Ds\Pair Object          (              [key] => a              [value] => 10          )        [1] => Ds\Pair Object          (              [key] => b              [value] => 20          )        [2] => Ds\Pair Object          (              [key] => c              [value] => 30          )        [3] => Ds\Pair Object          (              [key] => d              [value] => 40          )        [4] => Ds\Pair Object          (              [key] => e              [value] => 50          )    )    Map after reducing to single element  int(120000000)  
Reference: https://www.php.net/manual/en/ds-map.reduce.php

Next Article
PHP | Ds\Map reduce() Function

R

R_Raj
Improve
Article Tags :
  • Web Technologies
  • PHP
  • PHP-function
  • PHP-ds_map

Similar Reads

    PHP | DsSet reduce() Function
    The Ds\Set::reduce() function is an inbuilt function in PHP which is used to reduce the set to a single value by applying operations using the callback function. Syntax: mixed public Ds\Set::reduce ( callable $callback [, mixed $initial ] ) Parameters: This function accepts two parameters as mention
    2 min read
    PHP | DsVector reduce() Function
    The Ds\Vector::reduce() function is an inbuilt function in PHP which reduce the vector to a single value by applying operations in the callback function. Syntax: mixed public Ds\Vector::reduce( $callback, $initial ) Parameters: This function accepts two parameters as mentioned above and described be
    2 min read
    PHP | DsSequence reduce() Function
    The Ds\Sequence::reduce() function is an inbuilt function in PHP which is used to reduce the sequence to a single value using a callback function. Syntax: mixed abstract public Ds\Sequence::reduce ( callable $callback [, mixed $initial ] ) Parameters: This function accepts two parameters as mentione
    2 min read
    PHP array_reduce() Function
    This inbuilt function of PHP is used to reduce the elements of an array into a single value that can be of float, integer or string value. The function uses a user-defined callback function to reduce the input array. Syntax: array_reduce($array, own_function, $initial) Parameters: The function takes
    2 min read
    PHP | DsDeque map() Function
    The Ds\Deque::map() function is an inbuilt function in PHP which is used to return the Deque with each element modified on the basis of operation performed as per the callback function. Syntax: public Ds\Deque::map( $callback ) : Ds\Deque Parameters: This function accepts single parameter $callback
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences