Current Location: Home> Latest Articles> How to easily implement key-value-to-array conversion using PHP's array_column function?

How to easily implement key-value-to-array conversion using PHP's array_column function?

M66 2025-05-11

In PHP, the array_column function is a very useful tool that can easily extract data from a column from a multidimensional array. This function is perfect for use when you need to convert a complex array into a key-value pair array. In this article, we will dive into how to use array_column to implement key-value conversion and show actual code examples.

1. Introduction to array_column function

The array_column function is a function in PHP used to extract single column data from a two-dimensional array. It receives three parameters:

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

  • $column_key : The key name of the column to be returned (if a string) or the index of the column (if a number).

  • $index_key : optional parameter. If this parameter is set, the returned array will use the column as the key to the new array.

2. The requirement to implement key-value-to-array conversion

Suppose you have an array containing user information, and each user's information contains the user's ID and name. We want to convert this array into an array of key-value pairs with user ID as key and user name as value.

The original data example is as follows:

 $users = [
    ['id' => 1, 'name' => 'Alice'],
    ['id' => 2, 'name' => 'Bob'],
    ['id' => 3, 'name' => 'Charlie']
];

We want the converted array to be:

 [
    1 => 'Alice',
    2 => 'Bob',
    3 => 'Charlie'
]

3. Use array_column to implement conversion

This requirement can be easily achieved using array_column . We can convert data into a key-value pair array by specifying id as index_key and name as column_key .

 $users = [
    ['id' => 1, 'name' => 'Alice'],
    ['id' => 2, 'name' => 'Bob'],
    ['id' => 3, 'name' => 'Charlie']
];

// use array_column Come to extract id and name
$usersAssociative = array_column($users, 'name', 'id');

print_r($usersAssociative);

Output result:

 Array
(
    [1] => Alice
    [2] => Bob
    [3] => Charlie
)

4. Code explanation

  • array_column($users, 'name', 'id') : The first parameter $users is the input array, the second parameter 'name' specifies the column we want to extract, and the third parameter 'id' specifies the column as the key of the new array.

  • array_column will iterate over the $users array and extract the 'id' and 'name' fields for each element, and build the result into an associative array with id as the key and name as the value.

5. Use scenarios

This key-value-to-array conversion is very suitable for the following scenarios:

  • Database query result processing : The results queried from the database are usually a multi-dimensional array. Using array_column can easily convert data into a key-value pair array for further processing.

  • Configuration Item Management : If you have a set of configuration items, using array_column can easily convert it into an array with the identifier of the configuration item as a key.

  • Data sorting and grouping : When processing large-scale data, use array_column to quickly organize data and group operations.

6. Things to note

  • If there are duplicate keys in the input array, array_column will override the previous value, retaining the last occurrence of the value.

  • If the column corresponding to $index_key is missing in some elements, array_column ignores these elements.

7. Summary

array_column is a very practical function in PHP. Through it, we can easily extract single column data from multidimensional arrays and implement key-value pair conversion of arrays. Its simplicity and efficiency make it widely used in many practical developments. Whether it is processing database query results or managing configuration items, array_column can provide great convenience.