Current Location: Home> Latest Articles> Differences in support for array_column in PHP version

Differences in support for array_column in PHP version

M66 2025-04-28

Title: What are the differences in support of array_column function in different PHP versions? How to choose the appropriate usage based on the version?

array_column is a very useful function introduced in PHP 5.5.0. It can extract the value of a column from a multidimensional array. When processing large-scale data, array_column provides developers with a very convenient way to operate. However, there are some differences in the support of this function by different PHP versions, and understanding these differences is very important for developers to upgrade PHP versions or develop applications in different versions of PHP environments. This article will introduce in detail the differences in the support for array_column function in different PHP versions, and give suggestions on how to choose applicable methods based on the version.

1. Basic usage of array_column function

First, let's review the basic usage of the array_column function. In PHP 5.5.0 and later, array_column can be used to extract data from a column from a multidimensional array. Its basic syntax is as follows:

 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 (can be a string or an integer).

  • $index_key : Optional parameter, if set, the resulting array will use this key value as index.

Example:

 $data = [
    ['id' => 1, 'name' => 'John', 'age' => 23],
    ['id' => 2, 'name' => 'Jane', 'age' => 21],
    ['id' => 3, 'name' => 'Doe', 'age' => 25],
];

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

Output:

 Array
(
    [0] => John
    [1] => Jane
    [2] => Doe
)

2. Different PHP versions support the array_column function

Although array_column was introduced in PHP 5.5.0, there are still some differences between different PHP versions, especially when processing specific types of data.

2.1 PHP 5.5.x

PHP 5.5.x is the first version introduced by the array_column function, so this version supports functions relatively basic. It can process ordinary multi-dimensional arrays normally and extract the specified columns.

  • Support : Extract specified columns from multidimensional arrays.

  • Limitations : Complex key-value pairs are not supported, such as array keys being objects or some special types, which may not work properly.

2.2 PHP 5.6.x

PHP 5.6.x does not have much improvement on array_column , but it fixes some bugs in PHP 5.5.x, making it more stable when dealing with multidimensional arrays.

  • Support : Same as PHP 5.5.x.

  • Improvement : Some edge cases were dealt with stability issues.

2.3 PHP 7.x

PHP 7.x has made a lot of optimizations in terms of performance and has made some enhancements to the array_column function. Especially when processing large-scale data, performance has been significantly improved.

  • Support : Same as PHP 5.5/5.6, it supports extracting columns in multi-dimensional arrays.

  • Improvement : Improved performance, optimized memory usage, and more efficient when handling large arrays.

2.4 PHP 8.x

In PHP 8.0, the array_column function has been significantly improved. Especially when dealing with null values ​​and objects as column data, PHP 8.x enhances support for special data types.

  • Support : Support more data types, such as objects, null , etc.

  • Improvement : PHP 8 introduces better compatibility for handling columns containing null values.

  • Change : PHP 8.0 introduces support for null keys, allowing null as the values ​​of $column_key and $index_key .

2.5 PHP 8.1/8.2

In PHP 8.1 and PHP 8.2, array_column 's performance and compatibility are further enhanced and edge issues are fixed for some extreme cases. In addition, PHP 8.1 introduces support for enum types, making array_column perform better when processing arrays containing enums.

  • Support : Consistent with PHP 8.0, more data types are supported.

  • Improvements : Enhanced support for enum types, increasing compatibility for extreme types.

3. How to choose the appropriate usage according to the version?

Depending on the version of PHP, it is important to choose the appropriate usage of array_column . Here are some suggestions for choosing usage based on version:

  1. If you use PHP 5.5.x to PHP 5.6.x , you need to note that the function of array_column is relatively basic. When dealing with complex data such as objects or columns containing null values, it is recommended to replace array_column by manually processing the array to avoid incompatibility issues.

  2. If using PHP 7.x , you can use array_column with confidence, especially when you need to deal with large-scale arrays. It performs better in performance, but still needs attention to the handling of special data types.

  3. If using PHP 8.x or later , you can take advantage of the full functionality of array_column . Especially for arrays containing complex data types such as objects, null , or enumerations, PHP 8.x provides better compatibility and performance.

  4. When processing complex data , it is recommended to use array_column in PHP 8.x and above to better handle null values, objects, or other special data types.

4. Example: Use array_column in different PHP versions

Example in PHP 7.x:

 $data = [
    ['id' => 1, 'name' => 'John', 'age' => 23],
    ['id' => 2, 'name' => 'Jane', 'age' => 21],
    ['id' => 3, 'name' => 'Doe', 'age' => 25],
];

$ages = array_column($data, 'age'); // extract 'age' List
print_r($ages);

Example in PHP 8.x:

 $data = [
    ['id' => 1, 'name' => 'John', 'age' => null],
    ['id' => 2, 'name' => 'Jane', 'age' => 21],
    ['id' => 3, 'name' => 'Doe', 'age' => 25],
];

$ages = array_column($data, 'age'); // extract 'age' List
print_r($ages);

In PHP 8.x, when processing columns containing null values, array_column will be able to properly retain null values, which may be lost in lower versions of PHP.


Summarize

array_column is a very practical function that can extract data from a column from a multidimensional array. Different PHP versions have some differences in support for the array_column function, especially when dealing with complex data types and large-scale data. Depending on the characteristics of different PHP versions, choosing the appropriate usage can help you avoid potential compatibility issues and improve code stability and performance.