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

how to use git restore and completely ignore the changes: 3 scenarios (video)
Nov 27, 2022 programming

how to use git restore and completely ignore the changes: 3 scenarios (video)

we are going to talk about ways to ignore the edits we made in a git project and restore the changes by git restore ...

9 Min Read Read More
PHP DataTypes
Mar 21, 2023 programming

PHP DataTypes

Today we are going to learn about Data Types in PHP. Data types are another important subject in programming. The better you know the data types you are working with, the more skillful you become. ...

5 Min Read Read More
PHP isset and empty
Mar 21, 2023 programming

PHP isset and empty

Today we are going to learn about isset and empty in PHP. isset and empty are two functions that help us check the existence of a value. They are way more useful than you think. ...

5 Min Read Read More
PHP upload file
Feb 15, 2023 programming

PHP upload file

Today we are going to talk about uploading a file in PHP. Uploading a file is simple but it's very important to know how to handle uploading the files correctly and securely. ...

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