Backend Development 5 min read

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

This article explains PHP's array_column() function, available since version 5.5, showing its syntax, parameters, return values, and practical code examples for extracting specific keys from multidimensional arrays and optionally using an index key to create associative results.

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 the values of a specific key from a multidimensional array, which can be done using the array_column() function. This article introduces the usage of array_column() and provides code examples.

The array_column() function is available from PHP version 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. 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 of 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 multidimensional array 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 )
?>

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

Further Extension:

The array_column() function can also use the optional $index_key parameter to specify a key that will become the index of the returned array. The example below extracts the "name" values while using the "id" field as the index:

<?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 the index and 'name' as the value
$result = array_column($users, 'name', 'id');
print_r($result);
// Result: Array ( [1] => John [2] => Jane [3] => Smith )
?>

In this code, the $index_key parameter is set to "id", so the resulting associative array uses the user IDs as keys and the corresponding names as values.

Summary:

The array_column() function is a very practical and convenient tool that simplifies extracting specific key values from multidimensional arrays in PHP. With the examples provided, you should now have a deeper understanding of how to use array_column() effectively.

BackendPHParray_columnmultidimensional arrayphp tutorial
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.