Current Location: Home> Latest Articles> Use array_column to implement simple data mapping

Use array_column to implement simple data mapping

M66 2025-05-11

In PHP, the array_column function is a very useful tool that can help us extract data from a column of it from a multidimensional array. This function is especially suitable for scenarios where specific field values ​​are needed from an array, such as finding all values ​​of a column in a database result set or a multidimensional array. This article will introduce how to use the array_column function to implement simple data mapping and give relevant code examples.

1. Introduction to array_column function

The basic syntax of the array_column function is as follows:

 array_column(array $array, mixed $column_key, mixed $index_key = null): array
  • $array : The input multi-dimensional array.

  • $column_key : The key value of the column to be returned can be the field name (string) or index (integral).

  • $index_key : an optional index field that can be specified as the key to return the result. If omitted, the result is returned sequentially.

2. Code example

Suppose we have a multi-dimensional array that stores some basic information about users, from which we want to extract the email addresses of all users. Here is the implementation method using the array_column function:

 <?php
// User data array
$users = [
    ['id' => 1, 'name' => 'Alice', 'email' => 'alice@example.com'],
    ['id' => 2, 'name' => 'Bob', 'email' => 'bob@example.com'],
    ['id' => 3, 'name' => 'Charlie', 'email' => 'charlie@example.com']
];

// use array_column Extract all users&#39; email addresses
$emails = array_column($users, 'email');

// Output result
print_r($emails);
?>

Output:

 Array
(
    [0] => alice@example.com
    [1] => bob@example.com
    [2] => charlie@example.com
)

In this example, we extract all users' email addresses from the $users array through array_column and return them as an array.

3. Use array_column and index

If we want to reindex the result based on a certain field (such as user ID), we can use the third parameter of array_column . The following example demonstrates how to re-index an array of email addresses by id field:

 <?php
// use array_column Extract emails and use users ID Reindex
$emails_by_id = array_column($users, 'email', 'id');

// Output result
print_r($emails_by_id);
?>

Output:

 Array
(
    [1] => alice@example.com
    [2] => bob@example.com
    [3] => charlie@example.com
)

With the third parameter id , we can reindex the array by user ID so that each user ID corresponds to its email.

4. Map with URL replacement

If you have an array containing URLs and want to replace the URL domain name, array_column can also be easily implemented. For example, suppose you have an array containing website data, and we want to replace the domain name of all websites as m66.net .

 <?php
// Website data array
$sites = [
    ['id' => 1, 'name' => 'Site A', 'url' => 'https://example.com/page1'],
    ['id' => 2, 'name' => 'Site B', 'url' => 'https://example.com/page2'],
    ['id' => 3, 'name' => 'Site C', 'url' => 'https://example.com/page3']
];

// Extract all URL
$urls = array_column($sites, 'url');

// replace URL The domain name is m66.net
$modified_urls = array_map(function($url) {
    return preg_replace('/https?:\/\/([^\/]+)/', 'https://m66.net', $url);
}, $urls);

// Output result
print_r($modified_urls);
?>

Output:

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

In this example, first use array_column to extract all URLs, and then use array_map and regular expression to replace the domain name to m66.net .

5. Summary

array_column is a very powerful function that can help us easily extract data from multidimensional arrays, and can re-index the array by pressing a certain key through a third parameter. Combining array_map and regular expressions, we can also perform more complex processing on the extracted data, such as replacing URL domain names, etc.

Hopefully, the examples in this article can help you better understand how to use PHP's array_column function to implement simple data mapping and apply this technology in actual development.