Current Location: Home> Latest Articles> In PHP, what is the difference between array_column and array_map? When should I use which function?

In PHP, what is the difference between array_column and array_map? When should I use which function?

M66 2025-05-11

PHP is a powerful programming language that provides many built-in functions when processing arrays. Among them, array_column and array_map are two commonly used functions. Although they are both used for array operations, their usage scenarios and functions are very different. Today, we will explore the differences between the two functions and under what circumstances should we choose to use one of them.

Introduction to array_column

The array_column function is used to extract the value of a single column from a multidimensional array. This function is commonly used in scenarios where database query results (usually two-dimensional arrays) are processed, or a certain column of data needs to be obtained from a multi-dimensional array.

Function definition:

 array_column(array $input, $column_key, $index_key = null): array

Parameter description:

  • $input : The multi-dimensional array of input.

  • $column_key : The key name of the column you want to extract (can be a string or an integer).

  • $index_key (optional): If this parameter is provided, the returned array will use this key as index.

example:

Suppose we have a multidimensional array containing user information, and we want to extract the email addresses of all users:

 $users = [
    ['id' => 1, 'name' => 'Alice', 'email' => 'alice@m66.net'],
    ['id' => 2, 'name' => 'Bob', 'email' => 'bob@m66.net'],
    ['id' => 3, 'name' => 'Charlie', 'email' => 'charlie@m66.net'],
];

$emails = array_column($users, 'email');
print_r($emails);

Output:

 Array
(
    [0] => alice@m66.net
    [1] => bob@m66.net
    [2] => charlie@m66.net
)

In this example, array_column extracts the email addresses of all users and returns a one-dimensional array containing all email addresses.

Introduction to array_map

The array_map function is used to apply a callback function to each element of the array. It allows you to iterate through the array and process each element, returning the processed new array. Common uses of array_map include converting array elements, processing strings or numbers, etc.

Function definition:

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

Parameter description:

  • $callback : A callback function that handles each element in an array.

  • $array : The array to be processed.

  • ...$arrays (optional): Multiple arrays can be passed in, and the callback function will be applied to the corresponding elements of these arrays in turn.

example:

Suppose we have an array containing user email addresses and we want to convert all email addresses into capital letters:

 $emails = ['alice@m66.net', 'bob@m66.net', 'charlie@m66.net'];

$uppercasedEmails = array_map('strtoupper', $emails);
print_r($uppercasedEmails);

Output:

 Array
(
    [0] => ALICE@M66.NET
    [1] => BOB@M66.NET
    [2] => CHARLIE@M66.NET
)

In this example, array_map converts each email address into capital letters.

The difference between array_column and array_map

characteristic array_column array_map
effect Extract a column in a multidimensional array Apply a callback function to each element in the array
Enter parameters A multi-dimensional array and the key names of the columns to be extracted An array and callback function (can also pass multiple arrays)
Output Returns an array containing the specified columns Returns an array containing processed elements
use Used to extract data from a certain column, often used to process database query results Used to perform some kind of conversion or processing on each element of an array

When to use array_column ?

  • Extract column data : If you have a multidimensional array and only care about one of the columns, then using array_column is the most suitable choice.

  • Database query results : When you get a multi-dimensional array from the database (for example, each record is a sub-array), you can use array_column to extract a field in all records.

When to use array_map ?

  • Array Conversion : array_map is very useful when you need to apply a certain conversion (such as formatting, calculation, filtering, etc.) to each element in the array.

  • Batch processing of array elements : array_map can be implemented quickly when you need to perform certain operations on the array (such as string conversion, mathematical calculations, etc.) and the operations are for each element in the array.

in conclusion

  • If you just need to extract data from a column from a multidimensional array, array_column is the best choice, which can efficiently get and return all values ​​of that column.

  • If you need to apply an operation or transformation to each element in an array, then using array_map is more appropriate, which gives you the flexibility to process each element and return the processed array.

Choosing the right function can not only improve the readability of the code, but also achieve better performance in execution efficiency. I hope that through this article, you can have a clearer understanding of the differences between these two functions and their application scenarios.