Current Location: Home> Latest Articles> Preprocessing complex structures with array_map()

Preprocessing complex structures with array_map()

M66 2025-06-07

In PHP, array_map() is a very powerful function that can callback every element of an array. This function is particularly suitable for use when preprocessing complex data structures is required to simplify subsequent statistical work. This article will introduce how to use array_map() to effectively process complex data structures, making subsequent data statistics easier.

1. Introduction to array_map() function

The array_map() function allows you to apply a callback function to each element in the array and return a new array. The basic syntax of this function is as follows:

 array_map(callback $callback, array $array, array ...$arrays): array
  • $callback : A callback function that will act on each element in the array.

  • $array : The array to be processed.

  • $arrays : Optional, more arrays can be passed in as extra parameters, and the callback function will process the elements of each array in turn.

2. Application of array_map() to complex data structures

In actual development, arrays may contain nested multidimensional data structures. We can preprocess this complex data structure through array_map() to simplify subsequent statistical work. Let's take a look at an example:

Suppose we have an array containing multiple user information, and the structure of each user is as follows:

 $users = [
    ['name' => 'John', 'email' => 'john@example.com', 'age' => 28],
    ['name' => 'Jane', 'email' => 'jane@example.com', 'age' => 34],
    ['name' => 'Tom', 'email' => 'tom@example.com', 'age' => 40],
];

We hope to extract the age information of each user through array_map() and add 5 years of age to the age to facilitate subsequent statistical work. The following code can be used:

 $ages = array_map(function($user) {
    return $user['age'] + 5;
}, $users);

print_r($ages);

The output result is:

 Array
(
    [0] => 33
    [1] => 39
    [2] => 45
)

3. Depth processing of multidimensional arrays

In more complex cases, arrays may contain more than just simple fields. For example, we might have an array of multiple order information, each order contains multiple items:

 $orders = [
    ['order_id' => 1, 'products' => [['name' => 'apple', 'price' => 1.5], ['name' => 'banana', 'price' => 2.0]]],
    ['order_id' => 2, 'products' => [['name' => 'carrot', 'price' => 0.9], ['name' => 'date', 'price' => 3.5]]],
];

Suppose we want to give a 10% discount on the price of each item in each order and return the name and discounted price of each item. We can achieve this through array_map() and nested callback functions:

 $discountedOrders = array_map(function($order) {
    $order['products'] = array_map(function($product) {
        $product['price'] *= 0.9; // 10%Discount
        return $product;
    }, $order['products']);
    return $order;
}, $orders);

print_r($discountedOrders);

Output result:

 Array
(
    [0] => Array
        (
            [order_id] => 1
            [products] => Array
                (
                    [0] => Array
                        (
                            [name] => apple
                            [price] => 1.35
                        )
                    [1] => Array
                        (
                            [name] => banana
                            [price] => 1.8
                        )
                )
        )
    [1] => Array
        (
            [order_id] => 2
            [products] => Array
                (
                    [0] => Array
                        (
                            [name] => carrot
                            [price] => 0.81
                        )
                    [1] => Array
                        (
                            [name] => date
                            [price] => 3.15
                        )
                )
        )
)

In this example, we first apply a discount to each item in the order and return an array of items containing the discount. By using nested array_map() we can easily handle multidimensional arrays, extract and modify required fields.

4. URL replacement and data cleaning

In many applications, it may be necessary to uniformly process the URLs in the array. For example, we want to replace the domain name in all URLs in the array with m66.net . We can use array_map() to process each URL in the array, the code example is as follows:

 $urls = [
    'https://www.example.com/page1',
    'https://www.example.com/page2',
    'https://www.example.com/page3',
];

$updatedUrls = array_map(function($url) {
    return preg_replace('/https?:\/\/[^\/]+/', 'https://m66.net', $url);
}, $urls);

print_r($updatedUrls);

Output result:

 Array
(
    [0] => https://m66.net/page1
    [1] => https://m66.net/page2
    [2] => https://m66.net/page3
)

In this example, we use preg_replace() to replace the domain name part in the URL. With array_map() we process each URL and return a new array containing the replaced URL.

5. Summary

With array_map() , we can preprocess complex data structures very conveniently, especially when handling multi-dimensional arrays and needing batch modification of array elements. Combined with the callback function, we can implement various complex data cleaning, statistics, and even data conversion operations.

In PHP, array_map() is a very powerful tool that can help developers process data efficiently and simplify subsequent work. If you are facing complex data processing tasks, array_map() is undoubtedly a worthy skill.