Navigate back to the homepage

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

Vishnu Damwala
November 15th, 2018 · 1 min read

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:

1array 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

1$records = [
2 [
3 'id' => 12,
4 'country' => 'India',
5 'capital' => 'New Delhi',
6 ],
7 'Spain' => [
8 'id' => 24,
9 'country' => 'Spain',
10 'capital' => 'Madrid',
11 ],
12 [
13 'id' => 91,
14 'country' => 'Israel',
15 'capital' => 'Jerusalem',
16 ],
17 'Netherlands' => [
18 'id' => 121,
19 'country' => 'Netherlands',
20 'capital' => 'Amsterdam',
21 ]
22];
23$countries = array_column($records, 'country');
24echo "<pre>";
25print_r($countries);
26echo "</pre>";

The above example will output:

1Array
2(
3 [0] => India
4 [1] => Spain
5 [2] => Israel
6 [3] => Netherlands
7)

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

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

The above example will output:

1Array
2(
3 [India] => New Delhi
4 [Spain] => Madrid
5 [Israel] => Jerusalem
6 [Netherlands] => Amsterdam
7)

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.

More articles from MeshWorld

Identify Unused Code Using Chrome DevTools

Chrome DevTools allows developers to optimize using Sources panel to identify unused code on your web page.

November 15th, 2018 · 2 min read

Programs Worth Practising for Absolute Beginners

List of much basic programs to start your coding journey which can be helpful in improving your logic and problem-solving skills.

November 15th, 2018 · 4 min read
© 2017–2020 MeshWorld
Link to $https://twitter.com/meshworldindiaLink to $https://facebook.com/meshworldindiaLink to $https://instagram.com/meshworld.india/