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 | DsSet filter() Function
Next article icon

PHP | Ds\Map filter() Function

Last Updated : 20 Mar, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

The Ds\Map::filter() function is an inbuilt function in PHP which is used to create a new map using the filter function. Syntax:

 public Ds\Map::filter( $callback )

Parameters: It contains a single parameter $callback which is an optional parameter and it returns True if the value should be included, False otherwise. Return value: This function returns a new map containing all the pairs for which either the callback returned True or all values that convert to True if a callback was not provided. Below programs illustrate the Ds\Map::filter() function in PHP: Program 1: 

php
<?php  // PHP program to illustrate the filter()  // function of Ds\map   // Creating a Map  $map = new \Ds\Map([     1 => "Welcome",     2 => "to",     3 => "Geeks",       4 => "for",     5 => "Geeks"]);               // Display new sequence using filter function  var_dump($map->filter(function($key, $val) {      return $key % 3 == 0;  }));   ?>  
Output:
object(Ds\Map)#3 (1) {   [0]=>   object(Ds\Pair)#2 (2) {     ["key"]=>     int(3)     ["value"]=>     string(5) "Geeks"   } }

Program 2: 

php
<?php  // PHP program to illustrate the filter()  // function of Ds\map   // Creating a Map  $map = new \Ds\Map([         1 => 10,          2 => 20,         3 => 30,          4 => 40,         5 => 50]);               // Display new sequence using filter function  var_dump($map->filter(function($key, $val) {      return $val % 20 == 0;  }));   ?>  
Output:
object(Ds\Map)#3 (2) {   [0]=>   object(Ds\Pair)#2 (2) {     ["key"]=>     int(2)     ["value"]=>     int(20)   }   [1]=>   object(Ds\Pair)#4 (2) {     ["key"]=>     int(4)     ["value"]=>     int(40)   } }

Reference: https://www.php.net/manual/en/ds-map.filter.php


Next Article
PHP | DsSet filter() Function

J

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

Similar Reads

    PHP | DsMap filter() Function
    The Ds\Map::filter() function is an inbuilt function in PHP which is used to create a new map using the filter function. Syntax: public Ds\Map::filter( $callback ) Parameters: It contains a single parameter $callback which is an optional parameter and it returns True if the value should be included,
    2 min read
    PHP | DsSet filter() Function
    The Ds\Set::filter() function is an inbuilt function in PHP which is used to create new set using filter function. Syntax: Ds\Set public Ds\Set::filter( $callback ) Parameters: This function accepts single parameter $callback which is optional and it returns True if the value should be included, Fal
    1 min read
    PHP | DsDeque filter() Function
    The Ds\Deque::filter() function is an inbuilt function in PHP which is used to filter out the elements from the deque based on the operation defined in the callback function. Syntax: public Ds\Deque::filter( $callback ) : Ds\Deque Parameters: This function accepts single parameter $callback which is
    2 min read
    PHP | filter_id() Function
    The filter_id() function is an inbuilt function in PHP which returns the filter ID of a specified filter name. It is used to get the filter id of the particular filter in PHP by using filter_id function by giving the name of the filter as input and get the associated id to it. Syntax: int filter_id(
    2 min read
    PHP | DsVector map() Function
    The Ds\Vector::map() function is an inbuilt function in PHP which is used to return the result of a callback after applying to each value in the vector. Syntax: Ds\Vector public Ds\Vector::map( $callback ) Parameters: This function accepts single parameter $callback which is to be applied to each ve
    2 min read
    PHP | DsVector filter() Function
    The Ds\Vector::filter() function is used to filter out the only elements which satisfy the condition defined in the callback function. After doing a filter on the vector, it will eliminate the elements which do not satisfy the condition mentioned in the function. Syntax: Ds\Vector public Ds\Vector::
    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