Current Location: Home> Latest Articles> How to use array_combine to cooperate with array_column to implement data reorganization?

How to use array_combine to cooperate with array_column to implement data reorganization?

M66 2025-06-07

In PHP, array_combine and array_column are two very commonly used array processing functions. They each have different uses, but when combined, it allows for a very powerful data reorganization function. Next, we will use a simple example to demonstrate how to use these two functions to reorganize the data.

Function introduction

array_combine

The array_combine function is used to create a new associative array, the key is the element of an array, and the value is the element of another array. When using this function, the two arrays are required to have the same number of elements, otherwise a warning will be thrown.

Function prototype:

 array_combine(array $keys, array $values): array
  • $keys : as the key to the new array.

  • $values : as the value of the new array.

array_column

The array_column function is used to extract the data of a specified column from a multidimensional array and return an array. It is ideal for extracting a column in a two-dimensional array, usually used to extract specific fields from database query results.

Function prototype:

 array_column(array $input, mixed $column_key, mixed $index_key = null): array
  • $input : The input array to operate on.

  • $column_key : The key name of the column to be extracted.

  • $index_key (optional): The key name used as the index of the result array.

Example: Use array_combine and array_column

Suppose we have a set of data about employees that contains the employee’s name, position, and ID. We want to reorganize this data into an associative array with employee ID as key and employee name as value.

Raw data:

 $employees = [
    ['id' => 1, 'name' => 'Alice', 'position' => 'Developer'],
    ['id' => 2, 'name' => 'Bob', 'position' => 'Designer'],
    ['id' => 3, 'name' => 'Charlie', 'position' => 'Manager']
];

Target:

We want to create a new array with employee id as key and employee name as value.

Code implementation:

 <?php
// Raw data
$employees = [
    ['id' => 1, 'name' => 'Alice', 'position' => 'Developer'],
    ['id' => 2, 'name' => 'Bob', 'position' => 'Designer'],
    ['id' => 3, 'name' => 'Charlie', 'position' => 'Manager']
];

// use array_column Extract all employeesIDand employee names
$ids = array_column($employees, 'id');  // Extract all employeesID
$names = array_column($employees, 'name');  // Extract all employees姓名

// use array_combine Combined ID Japanese name,Generate associative arrays
$employeeNames = array_combine($ids, $names);

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

Output result:

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

explain:

  1. We first extracted the id columns of all employees through array_column($employees, 'id') .

  2. Then, the name columns for all employees are extracted using array_column($employees, 'name') .

  3. Finally, we use array_combine($ids, $names) to use id as key and name as value to form a new associative array.

In this way, we can quickly reorganize the data according to different needs.

Summarize

The combination of array_combine and array_column functions can help us easily extract specific columns from two-dimensional arrays and recombinate them into an associative array. This approach is very suitable for data processing, especially when processing results from database query or other multidimensional arrays. Mastering the use of these two functions will make you more comfortable in PHP programming.

Other links

If you want to know more about PHP array operations, you can refer to the official PHP document: https://www.php.net/manual/zh/