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.

Avatar

A web geek, an industry experienced web developer & tutor/instructor residing in India 🇮🇳

Next
Previous

Related