

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Explain array_map() function in PHP
PHP offers various types of inbuilt functions to perform distinctive functionalities. The array_map() is an inbuilt function in PHP and it works with PHP array.
This function works in such a way that it sends every component of an array to a predefined function and returns an array with new values as modified by that function.
This function changes all elements of at least one array as indicated by some user-defined condition in a simple way.
Syntax
array_map (function name, array1,array2...)
Parameters
This function takes 2 compulsory parameters one is function name and another is an array and the rest are discretionary.
functionname(mandatory)
This parameter characterizes the name of the user-defined function as per which the values in the array to be changed.
array1(mandatory)
This parameter determines the array to be changed.
Example
<?php function add($arr){ return ($arr+ 2); } $arr1 = array(7, 6, 2, 4); print_r(array_map("add", $arr1)); ?>
Output
Array ( [0] => 9 [1] => 8 [2] => 4 [3] => 6 )
Explanation
In the above example, we have defined a function which takes the input as an array and adds 2 to every element of that array
- Related Questions & Answers
- Explain array_merge() function in PHP.
- Explain array_intersect() function in PHP.
- Explain str_split() function in PHP
- Explain substr() function in PHP
- array() function in PHP
- Sum 2D array in Python using map() function
- Explain variable / Array scope in PowerShell function.
- map count( ) function in C++
- Explain Encapsulation in PHP.
- Explain Polymorphism in PHP.
- Explain interface in PHP.
- Explain array_diff() in PHP
- map emplace_hint() function in C++ STL
- map erase() function in C++ STL
- map find() function in C++ STL