PHP array_map

Created with Sketch.

PHP array_map

Summary: in this tutorial, you will learn how to use the PHP array_map() function that creates a new array whose elements are the results of applying a callback to each element.

Introduction to the PHP array_map() function

Suppose that you have an array that holds the lengths of squares:

<?php

$lengths = [10, 20, 30];

Code language: HTML, XML (xml)

To calculate the areas of the squares, you may come up with the foreach loop like this:

<?php

$lengths = [10, 20, 30];

// calculate areas
$areas = [];

foreach ($lengths as $length) {
$areas[] = $length * $length;
}

print_r($areas);

Code language: HTML, XML (xml)

Output:

Array
(
[0] => 100
[1] => 400
[2] => 900
)

Code language: PHP (php)

The foreach iterates over the elements of the $lengths array, calculates the area of each square and adds the result to the $areas array.

Alternatively, you can use the array_map() function that achieves the same result:

<?php

$lengths = [10, 20, 30];

// calculate areas
$areas = array_map(function ($length) {
return $length * $length;
}, $lengths);

print_r($areas);

Code language: HTML, XML (xml)

In this example, the array_map() applies an anonymous function to each element of the $lengths array. It returns a new array whose elements are the results of the anonymous function.

From PHP 7.4, you can use an arrow function instead of an anonymous function like this:

<?php

$lengths = [10, 20, 30];

// calculate areas
$areas = array_map(
fn ($length) => $length * $length,
$lengths
);

print_r($areas);

Code language: HTML, XML (xml)

PHP array_map() function syntax

The following shows the array_map() function syntax:

array_map ( callable|null $callback , array $array , array ...$arrays ) : array

Code language: PHP (php)

The array_map() has the following parameters:

  • $callback – a callable to apply to each element in each array.
  • $array – is an array of elements to which the callback function applies.
  • $arrays – is a variable list of array arguments to which the callback function applies.

The array_map() function returns a new array whose elements are the result of the callback function.

This tutorial focuses on the following form of the array_map() function:

array_map ( callable $callback , array $array ) : array

Code language: PHP (php)

PHP array_map() function examples

Let’s take some more examples of using the array_map() function.

1) Using the PHP array_map() with an array of objects

The following defines a class that has three properites: $id, $username, and $email and a list of User objects:

<?php

class User
{
public $id;

public $username;

public $email;

public function __construct(int $id, string $username, string $email)
{
$this->id = $id;
$this->username = $username;
$this->email = $email;
}
}

$users = [
new User(1, 'joe', 'joe@phptutorial.net'),
new User(2, 'john', 'john@phptutorial.net'),
new User(3, 'jane', 'jane@phptutorial.net'),
];

Code language: HTML, XML (xml)

The following illustrates how to use the array_map() function to get a list of usernames from the the $users array:

<?php

class User
{
public $id;

public $username;

public $email;

public function __construct(int $id, string $username, string $email)
{
$this->id = $id;
$this->username = $username;
$this->email = $email;
}
}

$users = [
new User(1, 'joe', 'joe@phptutorial.net'),
new User(2, 'john', 'john@phptutorial.net'),
new User(3, 'jane', 'jane@phptutorial.net'),
];

$usernames = array_map(
fn ($user) => $user->username,
$users
);

print_r($usernames);

Code language: HTML, XML (xml)

Output:

Array
(
[0] => joe
[1] => john
[2] => jane
)

Code language: PHP (php)

2) Using a static method as a callback

The callback function argument of the array_map() can be a public method of a class. For example:

<?php

class Square
{
public static function area($length)
{
return $length * $length;
}
}

$lengths = [10, 20, 30];

$areas = array_map('Square::area', $lengths);

print_r($areas);

Code language: HTML, XML (xml)

How it works.

  • First, define the Square class that has the area() public static method.
  • Second, create an array that holds the lengths of the three squares.
  • Third, calculate areas of the squares based on the lengths in the $lengths array using the static method Square::area.

Note that the syntax for passing a public static method to the array_map() function is as follows:

'className::staticMethodName'

Code language: JavaScript (javascript)

And the public static method must accept the array element as an argument.

Summary

  • Use the PHP array_map() method to create a new array by applying a callback function to every element of another array.

Leave a Reply

Your email address will not be published. Required fields are marked *