-
Notifications
You must be signed in to change notification settings - Fork 33
/
ease.php
42 lines (38 loc) · 990 Bytes
/
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
<?php
namespace collections;
/**
* Flattens a complex collection by mapping each ending leafs value to a key consisting of all previous indexes.
*
* __::ease(['foo' => ['bar' => 'ter'], 'baz' => ['b', 'z']]);
* // → '['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);
} elseif (!\is_object($value)) {
$map[$prefix . $index] = $value;
}
}
}