Using PHP's array_column() Function to Extract Values from Multidimensional Arrays
This article explains how the PHP array_column() function, available since version 5.5, can extract specific keys from multidimensional arrays, shows its syntax, parameter details, and provides practical code examples including using an index key for associative results.
In PHP programming, extracting values of a specific key from a multidimensional array can be efficiently done with the array_column() function. This article introduces the function's usage and provides detailed code examples.
The array_column() function is available from PHP 5.5.0 onward. It extracts the values of a specified column (key) from a multidimensional array and returns a one‑dimensional array containing those values.
Syntax:
array_column(array $input, mixed $column_key [, mixed $index_key = null])Parameter Description:
$input (required): The multidimensional array.
$column_key (required): The key whose values you want to extract.
$index_key (optional): The key to use as the index/key in the returned array.
Return Value:
Returns a one‑dimensional array containing the values from the specified column.
Code Example:
The following simple example demonstrates how to use array_column() to extract the 'name' values from a list of users:
<?php
$users = [
['id' => 1, 'name' => 'John', 'email' => '[email protected]'],
['id' => 2, 'name' => 'Jane', 'email' => '[email protected]'],
['id' => 3, 'name' => 'Smith', 'email' => '[email protected]'],
];
// Extract the 'name' column
$names = array_column($users, 'name');
print_r($names);
// Result: Array ( [0] => John [1] => Jane [2] => Smith )
?>This code creates a multidimensional array $users , then extracts the values associated with the 'name' key into $names and prints the resulting array.
Further Extension:
The array_column() function can also use the optional $index_key parameter to set a custom index for the returned array. The example below uses the 'id' field as the index and the 'name' field as the value:
<?php
$users = [
['id' => 1, 'name' => 'John', 'email' => '[email protected]', 'age' => 25],
['id' => 2, 'name' => 'Jane', 'email' => '[email protected]', 'age' => 30],
['id' => 3, 'name' => 'Smith', 'email' => '[email protected]', 'age' => 35],
];
// Use 'id' as index and 'name' as value
$result = array_column($users, 'name', 'id');
print_r($result);
// Result: Array ( [1] => John [2] => Jane [3] => Smith )
?>Here the $index_key parameter makes the 'id' values the keys of the resulting associative array, while the 'name' values become the corresponding values.
Summary:
The array_column() function is a highly useful and convenient tool in PHP that simplifies extracting specific key values from multidimensional arrays. With the explanations and examples provided, you should now have a deeper understanding of how to apply array_column() effectively in your code.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.