forked from maciejczyzewski/bottomline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathease.php
58 lines (54 loc) · 1.04 KB
/
ease.php
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
<?php
namespace collections;
/**
* Flattens a complex collection by mapping each ending leafs value to a key
* consisting of all previous indexes.
*
* **Usage**
*
* ```php
* __::ease([
* 'foo' => ['bar' => 'ter'],
* 'baz' => ['b', 'z']
* ]);
* ```
*
* **Result**
*
* ```
* [
* 'foo.bar' => 'ter',
* 'baz.0' => 'b',
* 'baz.1' => 'z'
* ]
* ```
*
* @param array $collection array of values
* @param string $glue glue between key path
*
* @return array flatten collection
*/
function ease(array $collection, $glue = '.')
{
$map = [];
_ease($map, $collection, $glue);
return $map;
}
/**
* Inner function for collections::ease
*
* @param array $map
* @param array $array
* @param string $glue
* @param string $prefix
*/
function _ease(&$map, $array, $glue, $prefix = '')
{
foreach ($array as $index => $value) {
if (\is_array($value)) {
_ease($map, $value, $glue, $prefix . $index . $glue);
} else {
$map[$prefix . $index] = $value;
}
}
}