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

Ultimate guide to RESTFul API Versioning in PHP Laravel: 2 solutions
Feb 15, 2023 programming

Ultimate guide to RESTFul API Versioning in PHP Laravel: 2 solutions

API versioning is what you need if you want to change the behavior of your API or the structure and format of requests and responses. ...

17 Min Read Read More
Introduction to PHP and how to build your first web application in less than 10 minutes
Mar 21, 2023 programming

Introduction to PHP and how to build your first web application in less than 10 minutes

PHP is an open source scripting language for backend development. and we are going to build our very first PHP application together. ...

7 Min Read Read More
PHP class
Mar 22, 2023 programming

PHP class

Today we are going to talk about classes in PHP. This is an introduction to a series of posts that would cover the topic of object oriented programming in PHP. class is the first one. ...

8 Min Read Read More
PHP Array Unique
Feb 14, 2023 programming

PHP Array Unique

Today we are going to talk about array unique function in PHP. It's a very good way to remove duplicated values from an array and change it to an array of unique values. ...

5 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 ...