In this tutorial we will look at the another built in array function that is array map function in php which helps modify or update an array
php array map function is used to iterate over the elements of array or number of arrays and it returns an array with new values depending on the definition of provided callback function
Recommended – Working with PHP Array Filter Function – Best Practices
Let’s look at the few example using php array_map()
function here but going to do that let’s see what is the syntax of array_map()
which we needs to follow:
Table of Contents
Syntax of using array_map() function
Here is the syntax of array_map() function:
array_map ( callable $callback , array $array1 [, array $... ] ) : array
If you see it is exactly opposite that the syntax of array filter function so here first we needs a callback function and then single or multiple arrays.
“Array Map function is useful when need to modify what gets returns”
How array map function works in PHP?
Initially to start array function requires a callback function which iterate over each element in each array and while interacting it map values from the array.
Finally it returns an array containing the updated values of first array from the list of provided arrays.
Keep in mind it does not returns the items from second and corresponding arrays.
So basically if you provide more than one array it will be used as arguments for the callback function.
Examples of using Array Map Function:
First let’s create array which is going to have collection of objects and then we will work accordingly.
<?php
class Post
{
public $title;
public $body;
public $author;
public $published;
public function __construct($title, $body, $author, $published)
{
$this->title = $title;
$this->body = $body;
$this->author = $author;
$this->published = $published;
}
}
$posts = [
new Post('Test Post 1', 'Body of post test 1', 'YK', true),
new Post('Test Post 2', 'Body of post test 2', 'YK', false),
new Post('Test Post 3', 'Body of post test 3', 'YK', true),
new Post('Test Post 4', 'Body of post test 4', 'AB', false)
];
If you see here I have added an $posts
array with four different Post
objects and each object has four different properties.
Now let’s see how we can use array map function to play around this $posts array
Use array map function to update elements from Array:
Let’s assume you want to update a particular property of each element from the array and get modified array returned
To demonstrate let’s make all post published from the $posts
array, here is how we can do that:
$posts = array_map(function ($post) {
$post->published = true;
return $post;
}, $posts);
var_dump($posts);
If you run the above code then you will see that all the properties of Post object has been updated
Modify array to only returns particular element from Array:
In some cases you might just want to get certain properties/elements form the array, like in the below example I just wanted to get the title of the post
$posts = array_map(function ($post) {
return $post->title;
}, $posts);
var_dump($posts);
Convert Array of Objects into Array:
This example is most useful when you work around the APIs and want to change values according to your needs.
Here is how you can cast object into array using array map function
$posts = array_map(function ($post) {
return (array) $post;
}, $posts);
var_dump($posts);
Conclusion:
I know this very simple tutorial but you know in some cases it is really useful to understand the core of it to utilize when you need.
The goal of this tutorial was to focus on providing you best practices from PHP so that it will help you while work on your PHP projects
So use array map function when you want to modify array according to your needs and requirements instead of using foreach loops
nice1 thx