Current Location: Home> Latest Articles> PHP 5.5 New array_column Function: Efficiently Extract a Column from Multi-dimensional Arrays

PHP 5.5 New array_column Function: Efficiently Extract a Column from Multi-dimensional Arrays

M66 2025-06-04

PHP 5.5 New array_column Function: Efficiently Extract a Column from Multi-dimensional Arrays

In PHP 5.5, a very useful new function called array_column was introduced. This function allows you to easily extract a specified column of data from a multi-dimensional array, significantly reducing the effort required to process complex arrays.

In everyday development, we often need to extract specific column data from multi-dimensional arrays, such as retrieving all user names or all order amounts. Traditionally, this would require looping through the array, but the array_column function allows you to achieve this with just one simple line of code.

Basic Usage of array_column

The basic syntax of the array_column function is as follows:

array_column ( array $array , mixed $column_key [, mixed $index_key = NULL ] )
  • $array: The multi-dimensional array from which data will be extracted.

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

  • $index_key (optional): The key to be used as the index for the extracted data.

Before using array_column, make sure your PHP version is updated to 5.5 or higher.

Example 1: Extract a Specific Column

Let's assume we have an array containing user information. We can use array_column to easily extract all user names.

$data = array(
    array('id' => 1, 'name' => 'John', 'age' => 25),
    array('id' => 2, 'name' => 'Mary', 'age' => 28),
    array('id' => 3, 'name' => 'Tom', 'age' => 30),
    array('id' => 4, 'name' => 'Lisa', 'age' => 23)
);

$names = array_column($data, 'name');
print_r($names);

Output:

Array
(
    [0] => John
    [1] => Mary
    [2] => Tom
    [3] => Lisa
)

In this example, the array_column function extracts the names of all users from the $data array and stores them in the $names array.

Example 2: Extract Data and Re-index It

In this example, we use $index_key to use the user names as the array keys and extract their corresponding ages.

$ages = array_column($data, 'age', 'name');
print_r($ages);

Output:

Array
(
    [John] => 25
    [Mary] => 28
    [Tom] => 30
    [Lisa] => 23
)

Here, the $ages array’s keys have become the user names, and the values are the corresponding ages. This makes the data more organized and allows for quick lookups.

Advantages of array_column

The main advantage of array_column is its simplicity and efficiency. Traditional methods require looping through arrays, whereas array_column allows you to extract the desired column data by simply specifying the column name, reducing the amount of code you need to write.

Furthermore, if the column data appears multiple times in the array, array_column will return the value from the last occurrence. If you need to retrieve all occurrences, you can use the third parameter of the function to set an index key.

Conclusion

The array_column function is an important feature introduced in PHP 5.5 that helps developers efficiently and concisely handle multi-dimensional arrays. If your PHP version is already updated to 5.5 or higher, it is highly recommended that you start using this function to simplify your array operations. By using it, you can easily extract specific column data without having to write complex loops, thus improving code readability and maintainability.