MeshWorld India LogoMeshWorld.
PHPTutorial3 min read

How to Get Values from Multi-dimensional Arrays with array_column()

Listen to ArticleAI Speech
~3 min read narration
100%
How to Get Values from Multi-dimensional Arrays with array_column()

array_column() is an inbuilt PHP function which can be used to get the values from a single column from the given multi-dimensional array or an array of objects.

Syntax

array array_column ( array $input , mixed $column_key [, mixed $index_key = NULL ] )

Support: (PHP 5>=5.5.0, PHP 7)

Parameters for array_column()

$input

  • The first parameter is mandatory.
  • It should be a multi-dimensional array or an array of objects from which to fetch/retrieve a column of values from.
  • With an array of objects, public properties can be directly fetched.
  • Whereas for protected or private properties, the class must implement both the __get() and __isset() methods.

$column_key

  • The second parameter is also mandatory.
  • It should be the column key for which values are to be fetched from a multi-dimensional array or an array of objects.
  • This value may be an integer key of the column you wish to retrieve, or it may be a string key name for an associative array or property name for an array of objects.

$index_key

  • The third parameter is an optional one.
  • The column whose values are used as the index/keys for the returned/resulting array.
  • This value may be the integer key of the column, or it may be the string key name.

Return Type

The return type of array_column() function is an another resultant array.

array_column() function returns a 1-D array which contains values from a single column of the given array, which is identified by a column_key.

Optionally, an index_key may also be provided to index the values in the resulted array by the values from the index_key column of the given array.

Example #1 Get the column of countries from a record-set

$records = [
    [
        'id' => 12,
        'country' => 'India',
        'capital' => 'New Delhi',
    ],
    'Spain' => [
        'id' => 24,
        'country' => 'Spain',
        'capital' => 'Madrid',
    ],
    [
        'id' => 91,
        'country' => 'Israel',
        'capital' => 'Jerusalem',
    ],
    'Netherlands' => [
        'id' => 121,
        'country' => 'Netherlands',
        'capital' => 'Amsterdam',
    ]
];
$countries = array_column($records, 'country');
echo "<pre>";
print_r($countries);
echo "</pre>";

The above example will output

Array
(
    [0] => India
    [1] => Spain
    [2] => Israel
    [3] => Netherlands
)

Example #2 Get the column of capitals from a record-set, indexed by their respective “country” column

echo "<pre>";
$capitalsWithCountries = array_column($records, 'capital', 'country');
print_r($$capitalsWithCountries);
echo "</pre>";

The above example will output

Array
(
    [India] => New Delhi
    [Spain] => Madrid
    [Israel] => Jerusalem
    [Netherlands] => Amsterdam
)

Conclusion

A good programming technique is trying to avoid the use of loops, nested loops & callback functions. The array_column() helps to reduce the code complexity and increases the code readability.

For more refer to the official documentation for same in manual.

Hope you find this helpful.

Reader Quality Feedback

Did this technical guide help solve your problem?

Suggest Errata ($0)
Vishnu
Primary Author

Vishnu

Founder & Principal Architect at MeshWorld. Senior engineer and instructor specializing in AI agent systems, scalable web architecture, and modern development workflows.

Explore Author Archive
Compute Fuel & Open Testbed
100% Independent & Verified

Fuel High-Density, Zero-Fluff Engineering Deep-Dives

Every guide on MeshWorld is validated on physical Linux nodes and reproducible testbeds. If this article saved you hours of debugging or unblocked production, consider funding our next cluster run.

Weekly Dispatch

Join MeshWorld Dispatch

Get practical tutorials, system blueprints, and curated AI engineering notes straight to your inbox. No fluff, zero spam.

Zero spam. 1-click unsubscribe anytime.Prefer RSS?
Curated Continuations

Up Next in This Domain.

Browse Full Archive