In PHP, array_column is a very useful function, especially when dealing with multidimensional arrays. It can extract the value of a column from a two-dimensional array and return a new array. However, when you use array_column on different types of arrays (such as numeric index arrays and associative arrays), you may find that they do not behave exactly the same. So, why is there such a difference?
A numeric index array is one of the most basic array types, and each element has an incremental index (for example, 0, 1, 2, etc.). In PHP, numeric index arrays are generally arranged in order.
$array = [
['id' => 1, 'name' => 'Tom'],
['id' => 2, 'name' => 'Jerry'],
['id' => 3, 'name' => 'Spike']
];
$names = array_column($array, 'name');
print_r($names);
Output:
Array
(
[0] => Tom
[1] => Jerry
[2] => Spike
)
In the above example, array_column extracts the value corresponding to the name key of each element in the array and returns an array of numeric indexes. Because the original array is an associative array, the values will be extracted in order when processed by array_column .
Unlike numeric index arrays, each element of an associative array has a specified key instead of the default numeric index. When you use array_column for associative arrays, the performance will be different.
$array = [
['id' => 1, 'name' => 'Tom'],
['id' => 2, 'name' => 'Jerry'],
['id' => 3, 'name' => 'Spike']
];
$names = array_column($array, 'name', 'id');
print_r($names);
Output:
Array
(
[1] => Tom
[2] => Jerry
[3] => Spike
)
In this example, in addition to extracting the value of the name column, array_column also uses the value of the id column as the key to the new array. Therefore, what is returned is an associative array whose key comes from the id value of the original array.
Number index array:
When array_column works on a numeric index array, a new index array will be generated according to the order of the original array. The keys for each new array are automatically generated and incremented from 0.
Associative array:
When the original array is an associative array, array_column will hold the key of the original array, or if a different key is specified (such as the id above), this specified key will be used as the key of the new array.
Specify the index:
array_column allows you to specify an array column as an index. This means that you can not only extract the values of a specific column, but also control the keys that return the array. This is very useful for building arrays with specific key values, especially when you need to index data by specific fields.
The behavior of array_column in numeric index arrays and associative arrays is different, mainly reflected in the returned array key value. For numeric index arrays, it uses numeric indexes by default; for associative arrays, it retains the original index or uses the index column you specified. Understanding these differences can help you better use this function in actual development and avoid unmet results.