-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayDotHelpers.php
More file actions
65 lines (56 loc) · 1.39 KB
/
ArrayDotHelpers.php
File metadata and controls
65 lines (56 loc) · 1.39 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
if( ! function_exists('array_dot') )
{
/**
* Flatten a multi dimensional array into a flat with dots
*
* @param array $array
* @param string $prefix
* @return array
*/
function array_dot(array $array, $prefix = '')
{
$new = [];
foreach($array as $key => $value) {
if(is_array($value)) {
$new = array_merge($new, array_dot($value, $prefix.$key.'.'));
continue;
}
$new[$prefix.$key] = $value;
}
return $new;
}
}
if( ! function_exists('array_undot') )
{
/**
* Turn array dot notation into a multidimensional array
*
* @param array $dotted The dotted array
* @param array $root The root array to append the unflattend array to.
* @return array
*/
function array_undot(array $dotted, array $root = [])
{
foreach($dotted as $key => $value) {
array_undot_element($root, $key, $value);
}
return $root;
}
/**
* Undot a single element
*
* @param array $root
* @param string $string
* @param mixed $value
* @return array
*/
function array_undot_element(&$root, $string, $value)
{
$explode = explode('.', $string);
foreach($explode as $step) {
$root = &$root[$step];
}
$root = $value;
}
}