Skip to content

Commit 44aed52

Browse files
committed
Provide array_dot if it does not exist
1 parent cb71e14 commit 44aed52

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/ArrayHelpers.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,24 @@ public function set(&$array, $key, $value)
5050
$array[array_shift($keys)] = $value;
5151
return $array;
5252
}
53+
54+
/**
55+
* Flatten a multi-dimensional associative array with dots.
56+
*
57+
* @param array $array
58+
* @param string $prepend
59+
* @return array
60+
*/
61+
public static function dot($array, $prepend = '')
62+
{
63+
$results = [];
64+
foreach ($array as $key => $value) {
65+
if (is_array($value) && ! empty($value)) {
66+
$results = array_merge($results, static::dot($value, $prepend.$key.'.'));
67+
} else {
68+
$results[$prepend.$key] = $value;
69+
}
70+
}
71+
return $results;
72+
}
5373
}

src/array_undot.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,17 @@ function array_undot(array $dotNotationArray)
1515
return (new ArrayHelpers())->undot($dotNotationArray);
1616
}
1717
}
18+
19+
if (!function_exists('array_dot')) {
20+
/**
21+
* Flatten a multi-dimensional associative array with dots.
22+
*
23+
* @param array $dotNotationArray
24+
*
25+
* @return array
26+
*/
27+
function array_dot(array $dotNotationArray)
28+
{
29+
return (new ArrayHelpers())->dot($dotNotationArray);
30+
}
31+
}

0 commit comments

Comments
 (0)