Skip to content

Commit ddd0cb3

Browse files
committed
v1.0
1 parent 530fe97 commit ddd0cb3

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed

composer.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
}

src/DotHelper.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
}

src/UndotHelper.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
}

0 commit comments

Comments
 (0)