Free cookie consent management tool by TermsFeed Generator PHP array map | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
PHP array map

PHP array map

Last Updated on Feb 14, 2023

What is array map?

array_map is a function in php that lets you apply a function to the elements of the given arrays and returns an array with changed elements.

and the syntax is like this:

array_map(function, array);

Predefined Function

The function can be a predefined function. In this case you should write the name of the function like a string.

function addHashtag($item){
    return '#' . $item;
}
$names = ['vitto','simon','pratham','hassib','thomas','tom'];
$hashtagged = array_map("addHashtag",$names);
// hashtagged array is:
// ['#vitto','#simon','#pratham','#hassib','#thomas','#tom'];

Anonymous Function

Or it can be an anonymous function.

$names = ['vitto','simon','pratham','hassib','thomas','tom'];
$uppercased = array_map(function($item){
    return strtoupper($item);
},$names);
// uppercased array is:
// ['VITTO','SIMON','PRATHAM','HASSIB','THOMAS','TOM'];

array_map helps you avoid writing something like this

$names = ['vitto','simon','pratham','hassib','thomas','tom'];
$uppercasedNames = [];
foreach($names as $name){
    // add the uppercased name to the array
    $uppercasedNames[] = strtoupper($name);
}

So instead you can write something like this

$names = ['vitto','simon','pratham','hassib','thomas','tom'];
$uppercased = array_map(function($item){
    return strtoupper($item);
},$names);

do you see how shorter and cleaner it is?

Conclusion

Now you know about array map in PHP.

I recommend you to open a PHP files and try to apply a function to every elements in an array.

If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!

Key takeaways

  •  array map function in PHP
  • predefined and anonymous function in array map

Category: programming

Tags: #php #array

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

PHP Database
Mar 22, 2023 programming

PHP Database

Today we are going to talk about working with a database in PHP. working with databases is a very important part of web applications. you should be able to store data and read them. ...

14 Min Read Read More
PHP Exec
Mar 22, 2023 programming

PHP Exec

Today we are going to talk about exec in PHP. Did you know we can run system commands through PHP? Well not only it’s possible but also it’s very easy. You can do it with exec function ...

3 Min Read Read More
PHP Array Replace
Feb 14, 2023 programming

PHP Array Replace

Today we are going to talk about array replace function in PHP. Array replace is another array function that is very simple yet a very powerful function. ...

8 Min Read Read More
New PHP Tools You’ve Probably Never Heard Of
Jul 15, 2025 programming

New PHP Tools You’ve Probably Never Heard Of

The PHP ecosystem is constantly evolving, with fresh packages, libraries, and tools emerging that aim to solve old problems in new ways. There’s a growing landscape of lesser-known tools quietly gaining traction within the PHP community. ...

10 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...