Backend Development 4 min read

Using PHP’s array_column() Function to Extract Values from Multidimensional Arrays

This article explains the PHP array_column() function introduced in version 5.5, detailing its syntax, parameters, return values, and provides multiple code examples showing how to extract specific keys from multidimensional arrays, including using an index key to create associative arrays.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP’s array_column() Function to Extract Values from Multidimensional Arrays

In PHP programming, we often need to extract values of a specific key from a multidimensional array, which can be done using the array_column() function. This article details the usage and code examples of array_column() .

The array_column() function is available from PHP 5.5.0 onward. It extracts the values of a specified 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. Multidimensional array.

$column_key : Required. The key name to extract.

$index_key : Optional. Used as the index/key of the returned array.

Return value:

Returns a one‑dimensional array containing the values of the specified key.

Code example:

The following simple example demonstrates how to use array_column() to extract the values of the name key from a multidimensional array:

1, 'name' => 'John', 'email' => '[email protected]'],
    ['id' => 2, 'name' => 'Jane', 'email' => '[email protected]'],
    ['id' => 3, 'name' => 'Smith', 'email' => '[email protected]'],
];

// Extract the 'name' values
$names = array_column($users, 'name');
print_r($names);
// Result: Array ( [0] => John [1] => Jane [2] => Smith )
?>

The code creates a multidimensional array $users , then uses array_column() to extract the values associated with the name key into $names , and finally prints the resulting array.

Further extension:

The array_column() function can also use the $index_key parameter to specify a key as the index of the returned array. The following example extracts the name values while using the id key as the index:

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],
];

// Extract 'name' values indexed by 'id'
$result = array_column($users, 'name', 'id');
print_r($result);
// Result: Array ( [1] => John [2] => Jane [3] => Smith )
?>

Here the $index_key parameter sets the id key as the index, producing an associative array where each id maps to the corresponding name .

Conclusion:

The array_column() function is a practical and convenient tool that simplifies extracting specific key values from multidimensional arrays in PHP. The explanations and examples above should give you a deeper understanding of how to use it effectively.

backendPHPData Extractionarray_columnmultidimensional array
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.