array_map() is a powerful function in PHP that allows you to apply a callback function to each element of an array and return the results. Typically, when using array_map(), you pass a single array as an argument, and the callback function is applied to each element of that array. However, array_map() also supports receiving multiple arrays as arguments, enabling you to operate on the elements of several arrays at once.
This article will demonstrate how to use multiple arrays in array_map() through a practical case study, providing code examples along the way.
The basic syntax of the array_map() function is as follows:
array_map(callable $callback, array $array1, array $array2, ..., array $arrayN)
callable $callback: The callback function that will be applied to each element of the arrays.
array $array1, array $array2, ..., array $arrayN: Multiple arrays as input. array_map() will pass the corresponding elements from each array to the callback function for processing.
It’s important to note that the result returned by array_map() is an array where each element is the result of applying the callback function.
Let’s assume we have two arrays, each containing numbers, and we want to add the corresponding elements from these arrays together. We can achieve this using array_map().
<?php
// Define two arrays
$array1 = [1, 2, 3, 4];
$array2 = [10, 20, 30, 40];
<p>// Use array_map() to perform addition on the arrays<br>
$result = array_map(function($a, $b) {<br>
return $a + $b;<br>
}, $array1, $array2);</p>
<p>// Output the result<br>
print_r($result);<br>
?><br>
Array
(
[0] => 11
[1] => 22
[2] => 33
[3] => 44
)
In this example, array_map() receives two arrays, $array1 and $array2, and applies the anonymous function to add each pair of corresponding elements. The resulting $result array contains the sum of each pair of elements.
Another common use case is to combine elements from two arrays, one containing strings and the other containing numbers, and output them in a formatted manner. For example, one array holds the names of fruits, while the other holds their prices, and we want to output them as formatted strings.
Let’s assume we have the following two arrays:
<?php
$names = ["Apple", "Banana", "Cherry", "Date"];
$prices = [2.5, 3.0, 2.8, 3.5];
<p>// Use array_map() to combine the arrays into formatted strings<br>
$formatted = array_map(function($name, $price) {<br>
return $name . " costs $" . number_format($price, 2);<br>
}, $names, $prices);</p>
<p>// Output the formatted result<br>
print_r($formatted);<br>
?><br>
Array
(
[0] => Apple costs $2.50
[1] => Banana costs $3.00
[2] => Cherry costs $2.80
[3] => Date costs $3.50
)
In this example, we combine each fruit name with its corresponding price to form a formatted string. The number_format() function ensures that the price is displayed with two decimal places. array_map() makes this operation very simple.
If you have more arrays and need to perform more complex operations, array_map() is still an ideal choice. Let’s say you need to calculate a new value based on elements from multiple arrays. You can flexibly use multiple arrays as input for your callback function.
For instance, let’s calculate the discounted price of products based on their names, original prices, and discount rates:
<?php
$products = ["Apple", "Banana", "Cherry", "Date"];
$prices = [2.5, 3.0, 2.8, 3.5];
$discounts = [0.1, 0.2, 0.15, 0.05];
<p>// Use array_map() to calculate discounted prices<br>
$discounted_prices = array_map(function($product, $price, $discount) {<br>
$discounted_price = $price * (1 - $discount);<br>
return $product . " now costs $" . number_format($discounted_price, 2);<br>
}, $products, $prices, $discounts);</p>
<p>// Output the discounted prices<br>
print_r($discounted_prices);<br>
?><br>
Array
(
[0] => Apple now costs $2.25
[1] => Banana now costs $2.40
[2] => Cherry now costs $2.38
[3] => Date now costs $3.33
)
This example shows how to use three arrays ($products, $prices, $discounts) together to calculate discounted prices and generate formatted output.
The array_map() function is a powerful tool for processing multiple arrays. It allows you to perform operations on the elements of multiple arrays in a very concise and efficient way. Whether you’re performing simple arithmetic calculations or more complex string manipulations, array_map() provides excellent support. By passing multiple arrays as arguments, you can easily process their elements within the callback function.
Related Tags:
array_map