Make 'toHaveKeys' accept multi-dimensional associative arrays

This commit is contained in:
Francescu Garoby
2022-04-29 09:54:12 +02:00
parent 6e120d60f6
commit 77b7181b08
4 changed files with 49 additions and 6 deletions

View File

@ -60,4 +60,26 @@ final class Arr
return $array;
}
/**
* Flatten a multi-dimensional associative array with dots.
*
* @param array<array-key, mixed> $array
*
* @return array<int|string, mixed>
*/
public static function dot(array $array, string $prepend = ''): array
{
$results = [];
foreach ($array as $key => $value) {
if (is_array($value) && count($value) > 0) {
$results = array_merge($results, static::dot($value, $prepend . $key . '.'));
} else {
$results[$prepend . $value] = $value;
}
}
return $results;
}
}