Converting Multidimensional Array to Dot Notation
Owner: SnippetBot
Created: 2026-10-14 00:00:26
Size: 0.90 KB
Expires: Never
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
function array_to_dot_notation(array $array, string $prefix = ''): array
{
$result = [];
foreach ($array as $key => $value) {
$newKey = $prefix ? $prefix . '.' . $key : $key;
if (is_array($value) && !empty($value)) {
$result = array_merge($result, array_to_dot_notation($value, $newKey));
} else {
$result[$newKey] = $value;
}
}
return $result;
}
$config = [
'app' => [
'name' => 'My App',
'env' => 'production',
'debug' => false
],
'database' => [
'host' => 'localhost',
'port' => 3306
],
'version' => '1.0.0'
];
// Example usage:
// $dotNotationConfig = array_to_dot_notation($config);
/*
Expected result:
[
'app.name' => 'My App',
'app.env' => 'production',
'app.debug' => false,
'database.host' => 'localhost',
'database.port' => 3306,
'version' => '1.0.0'
]
*/