I created an easy to use API to help businesses do incredible things with AI.
PHP

Making sense of PHP's array_map() function

Benjamin Crozat
Published on Nov 4, 2023 0 comments Edit on GitHub
Making sense of PHP's array_map() function

About array_map()

The array_map() function in PHP is extremely useful to write leaner code when transforming arrays and avoid lengthier loops with temporary variables and an additional indentation level.

When I was inexperienced with PHP, I had a hard time understanding the array_map() function. Now, things have changed, so let me desmystify this super handy function for you!

How to use array_map() in PHP

The array_map() takes a callable as its first parameter, and the array we want to transform as its second parameter. It then returns the a fresh array that has freshly been transformed.

Now, imagine we have an array of prices that we want to apply a discount to.

Before array_map(), we would use a code that looks like this:

function discount($price)
{
	return $price * (1 - 0.2);
}

$prices = [
	10, 20, 30, 40, 50,
];

$discounted_prices = [];

foreach ($prices as $price)
{
	$discounted_prices[] = discount($price);
}

This is good, but we can do better thanks to array_map():

function discount($price)
{
	return $price * (1 - 0.2);
}

$prices = [
	10, 20, 30, 40, 50,
];

$discounted_prices = array_map(discount(...), $prices);

$discounted_prices now contains:

[
	8,
	16,
	24,
	32,
	40,
];

Also, did you know that array_map() can also accept a closure (or anonymous function)?

$prices = [
	10, 20, 30, 40, 50,
];

$discounted_prices = array_map(function ($price) {
	return $price * (1 - 0.2);
}, $prices);

But wait, did you really think this is it? Why not use the arrow syntax on our closure?

$prices = [
	10, 20, 30, 40, 50,
];

$discounted_prices = array_map(
	fn ($price) => $price * (1 - 0.2),
	$prices
);

There you have it! Using array_map() can drastically help improving your code. Now, you can take a look at other similar functions such as array_filter() or array_reduce().

One more thing about array_map()

Ha! You thought that was really it, right? No! array_map() is such a useful function to use in PHP!

So, what you probably didn’t know is that it can accept an infinite amount of arrays to loop through. Here’s an example:

$products = [
	'Apple Watch',
	'iMac',
	'iPhone',
	'Mac Studio',
	'MacBook Pro',
];

$prices = [
	10, 20, 30, 40, 50,
];

$discounted_products = array_map(function ($product, $price) {
	return [
		'product' => $product,
		'discounted_price' => $price * (1 - 0.2)
	];
}, $products, $prices);

In this block of code, we build a unique multidimensional array out of two dinstinct ones and apply the discount.

[
	[
		'product' => 'Apple Watch',
		'discounted_price' => 8,
	],
	…
]

Be the first to comment!

Get help or share something of value with other readers!

Great deals for enterprise developers
  • Summarize and talk to YouTube videos. Bypass ads, sponsors, chit-chat, and get to the point.
    Try Nobinge →
  • Monitor the health of your apps: downtimes, certificates, broken links, and more.
    20% off the first 3 months using the promo code CROZAT.
    Try Oh Dear for free
  • Keep the customers coming; monitor your Google rankings.
    30% off your first month using the promo code WELCOME30
    Try Wincher for free →
The latest community links
- / -