Skip to content

Instantly share code, notes, and snippets.

@blood72
Last active April 12, 2021 18:38
Show Gist options
  • Save blood72/0c6ffd6a279f93bb890dac39f1acdd58 to your computer and use it in GitHub Desktop.
Save blood72/0c6ffd6a279f93bb890dac39f1acdd58 to your computer and use it in GitHub Desktop.
Return the values from a single column in the input array recursively
<?php
if (! function_exists('array_column_recursive')) {
/**
* Return the values from a single column in the input array recursively
*
* @param array $array
* @param int|string|null $columnKey
* @param int|string|null $indexKey
* @return array
*/
function array_column_recursive(array $array, $columnKey, $indexKey = null): array
{
$result = array_column($array, $columnKey, $indexKey);
foreach ($array as $value) {
if (is_array($value)) {
$recursive = array_column_recursive($value, $columnKey, $indexKey);
$result = is_null($indexKey)
? array_merge($result, $recursive)
: array_replace($result, $recursive);
}
}
return $result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment