Last active
April 12, 2021 18:38
-
-
Save blood72/0c6ffd6a279f93bb890dac39f1acdd58 to your computer and use it in GitHub Desktop.
Return the values from a single column in the input array recursively
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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