File tree Expand file tree Collapse file tree 3 files changed +130
-0
lines changed Expand file tree Collapse file tree 3 files changed +130
-0
lines changed Original file line number Diff line number Diff line change
1
+ {
2
+ "name" : " coding-saxony/array-helper" ,
3
+ "type" : " library" ,
4
+ "license" : " MIT" ,
5
+ "support" : {
6
+ "issues" : " https://github.com/coding-saxony/array-helper/issues" ,
7
+ "source" : " https://github.com/coding-saxony/array-helper"
8
+ },
9
+ "minimum-stability" : " dev" ,
10
+ "homepage" : " https://github.com/coding-saxony/array-helper" ,
11
+ "authors" : [
12
+ {
13
+ "name" : " Jonas Hanisch" ,
14
+ "email" : " jonas.hanisch@ffenergie.de" ,
15
+ "homepage" : " https://github.com/informatJonas" ,
16
+ "role" : " Developer"
17
+ }
18
+ ],
19
+ "require" : {
20
+ "php" : " ^8.0"
21
+ },
22
+ "require-dev" : {
23
+ "orchestra/testbench" : " ^4.0|^5.0|^6.0" ,
24
+ "facade/ignition" : " ^2.4.2"
25
+ },
26
+ "autoload" : {
27
+ "psr-4" : {
28
+ "CodingSaxony\\ ArrayHelper\\ " : " src/"
29
+ }
30
+ }
31
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace CodingSaxony \ArrayHelper ;
4
+
5
+ /**
6
+ * Class DotHelper
7
+ *
8
+ * @package CodingSaxony\ArrayHelper
9
+ */
10
+ class DotHelper
11
+ {
12
+ /**
13
+ * Flatten a multi-dimensional associative array with dots.
14
+ *
15
+ * @param array $array
16
+ * @param string $prepend
17
+ *
18
+ * @return array
19
+ */
20
+ public static function dot (array $ array , string $ prepend = '' ): array
21
+ {
22
+ $ results = [];
23
+
24
+ foreach ($ array as $ key => $ value ) {
25
+ if (is_array ($ value ) && !empty ($ value )) {
26
+ $ results = array_merge ($ results , static ::dot ($ value , $ prepend .$ key .'. ' ));
27
+ } else {
28
+ $ results [$ prepend .$ key ] = $ value ;
29
+ }
30
+ }
31
+
32
+ return $ results ;
33
+ }
34
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace CodingSaxony \ArrayHelper ;
4
+
5
+ /**
6
+ * Class UndotHelper
7
+ *
8
+ * @package CodingSaxony\ArrayHelper
9
+ */
10
+ class UndotHelper
11
+ {
12
+ /**
13
+ * Expands a dot notation array into a full multi-dimensional array.
14
+ *
15
+ * @param array $dotNotationArray
16
+ *
17
+ * @return array
18
+ */
19
+ public static function undot (array $ dotNotationArray ): array
20
+ {
21
+ $ array = [];
22
+
23
+ foreach ($ dotNotationArray as $ key => $ value ) {
24
+ self ::set ($ array , $ key , $ value );
25
+ }
26
+
27
+ return $ array ;
28
+ }
29
+
30
+ /**
31
+ * Set an array item to a given value using "dot" notation.
32
+ *
33
+ * If no key is given to the method, the entire array will be replaced.
34
+ *
35
+ * @param array $array
36
+ * @param string $key
37
+ * @param mixed $value
38
+ *
39
+ * @return array
40
+ */
41
+ private static function set (array &$ array , string $ key , mixed $ value ): array
42
+ {
43
+ if (is_null ($ key )) {
44
+ return $ array = $ value ;
45
+ }
46
+
47
+ $ keys = explode ('. ' , $ key );
48
+
49
+ while (count ($ keys ) > 1 ) {
50
+ $ key = array_shift ($ keys );
51
+ // If the key doesn't exist at this depth, we will just create an empty array
52
+ // to hold the next value, allowing us to create the arrays to hold final
53
+ // values at the correct depth. Then we'll keep digging into the array.
54
+ if (!isset ($ array [$ key ]) || !is_array ($ array [$ key ])) {
55
+ $ array [$ key ] = [];
56
+ }
57
+
58
+ $ array = &$ array [$ key ];
59
+ }
60
+
61
+ $ array [array_shift ($ keys )] = $ value ;
62
+
63
+ return $ array ;
64
+ }
65
+ }
You can’t perform that action at this time.
0 commit comments