Categories: PHP

3 Useful examples of using Array Map function in PHP – Best Practices

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:

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

Example of Using array map function to update elements from Array

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);
Example of modifying array to only returns particular element from Array

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);
Convert Array of Objects into Array

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

Yogesh Koli

Software engineer & Blogger lives in India, has 6+ years of experience working with the front-end and back-end web app development.

Recent Posts

Complete guide of using Laravel 6 Eloquent Subquery Enhancements

Learn How to use laravel frameworks new improved feature called Eloquent Subquery and get example…

8 months ago

Working with PHP Array Filter Function – Best Practices

Learn how to use php array filter function with easy and essential tutorial to filter…

8 months ago

How to add Access Modifiers with Constructor Parameters in TypeScript

Want to know how to refactor your Typescript class, Learn here utilising Typescript of the…

8 months ago

What is Access Modifiers and how to use Access Modifiers in TypeScript ?

What is Access Modifiers in typescript, how to use Access Modifiers, when to use them,…

9 months ago

Top 10 Super Useful Packages to Improve Laravel applications in 2019

This tutorial provide ultimate list of package those are top 10 on packagist and super…

9 months ago

How to secure pdf files In Laravel and allow access to authenticated users

This tutorial provides a perfect solution on how to protect and secure pdf file in…

9 months ago