Last active
January 23, 2023 13:59
-
-
Save medeirosinacio/91767f8bd7cffb437ed158ce5aa4c703 to your computer and use it in GitHub Desktop.
get all values from a multidimensional array, either by key or all.
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_value_recursive')) { | |
/** | |
* Retrieves values from a multidimensional array, either by key or all values. | |
*/ | |
function array_value_recursive(array $arr, ?string $key = null, bool $unique = true): array | |
{ | |
array_walk_recursive($arr, function ($v, $k) use ($key, &$val) { | |
if (is_null($key) || ($key && $k == $key)) { | |
$val[] = $v; | |
} | |
}); | |
return $unique ? array_unique($val ?? []) : $val ?? []; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment