Skip to content

Instantly share code, notes, and snippets.

@medeirosinacio
Last active January 23, 2023 13:59
Show Gist options
  • Save medeirosinacio/91767f8bd7cffb437ed158ce5aa4c703 to your computer and use it in GitHub Desktop.
Save medeirosinacio/91767f8bd7cffb437ed158ce5aa4c703 to your computer and use it in GitHub Desktop.
get all values from a multidimensional array, either by key or all.
<?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