File tree Expand file tree Collapse file tree 4 files changed +64
-12
lines changed Expand file tree Collapse file tree 4 files changed +64
-12
lines changed Original file line number Diff line number Diff line change 1
1
/vendor /
2
- composer.lock
2
+ composer.lock
3
+ .idea
4
+ * .cache
Original file line number Diff line number Diff line change 2
2
"name" : " divineomega/array_undot" ,
3
3
"description" : " array_undot (the opposite of the array_dot helper function) expands a dot notation array into a full multi-dimensional array." ,
4
4
"type" : " library" ,
5
- "require" : {
6
- "illuminate/support" : " ^5.1"
7
- },
8
5
"require-dev" : {
9
6
"phpunit/phpunit" : " ^7.0 || ^8.0" ,
10
- "satooshi /php-coveralls" : " ^2.0 "
7
+ "php-coveralls /php-coveralls" : " ^2.1 "
11
8
},
12
9
"license" : " LGPL-3.0-only" ,
13
10
"authors" : [
19
16
"autoload" :{
20
17
"files" : [
21
18
" src/array_undot.php"
22
- ]
19
+ ],
20
+ "psr-4" : {
21
+ "DivineOmega\\ ArrayUndot\\ " : " src/"
22
+ }
23
23
}
24
24
}
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace DivineOmega \ArrayUndot ;
4
+
5
+
6
+ class ArrayHelpers
7
+ {
8
+ /**
9
+ * Expands a dot notation array into a full multi-dimensional array
10
+ *
11
+ * @param array $dotNotationArray
12
+ * @return array
13
+ */
14
+ public function undot (array $ dotNotationArray )
15
+ {
16
+ $ array = [];
17
+ foreach ($ dotNotationArray as $ key => $ value ) {
18
+ $ this ->set ($ array , $ key , $ value );
19
+ }
20
+
21
+ return $ array ;
22
+ }
23
+
24
+ /**
25
+ * Set an array item to a given value using "dot" notation.
26
+ *
27
+ * If no key is given to the method, the entire array will be replaced.
28
+ *
29
+ * @param array $array
30
+ * @param string $key
31
+ * @param mixed $value
32
+ * @return array
33
+ */
34
+ public function set (&$ array , $ key , $ value )
35
+ {
36
+ if (is_null ($ key )) {
37
+ return $ array = $ value ;
38
+ }
39
+ $ keys = explode ('. ' , $ key );
40
+ while (count ($ keys ) > 1 ) {
41
+ $ key = array_shift ($ keys );
42
+ // If the key doesn't exist at this depth, we will just create an empty array
43
+ // to hold the next value, allowing us to create the arrays to hold final
44
+ // values at the correct depth. Then we'll keep digging into the array.
45
+ if (! isset ($ array [$ key ]) || ! is_array ($ array [$ key ])) {
46
+ $ array [$ key ] = [];
47
+ }
48
+ $ array = &$ array [$ key ];
49
+ }
50
+ $ array [array_shift ($ keys )] = $ value ;
51
+ return $ array ;
52
+ }
53
+ }
Original file line number Diff line number Diff line change 1
1
<?php
2
2
3
+ use DivineOmega \ArrayUndot \ArrayHelpers ;
4
+
3
5
if (!function_exists ('array_undot ' )) {
4
6
/**
5
7
* Process dot array will return undot array.
10
12
*/
11
13
function array_undot (array $ dotNotationArray )
12
14
{
13
- $ array = [];
14
- foreach ($ dotNotationArray as $ key => $ value ) {
15
- array_set ($ array , $ key , $ value );
16
- }
17
-
18
- return $ array ;
15
+ return (new ArrayHelpers ())->undot ($ dotNotationArray );
19
16
}
20
17
}
You can’t perform that action at this time.
0 commit comments