forked from app-insights-php/app-insights-php-bundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FlatArray.php
44 lines (35 loc) · 1019 Bytes
/
FlatArray.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
<?php
declare(strict_types=1);
/*
* This file is part of the App Insights PHP project.
*
* (c) Norbert Orzechowicz <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace AppInsightsPHP\Symfony\AppInsightsPHPBundle;
final class FlatArray
{
private $array;
public function __construct(array $array)
{
$this->array = $array;
}
public function __invoke(): array
{
return $this->flatterArray($this->array);
}
private function flatterArray(array $array, string $prefix = ''): array
{
$result = [];
foreach ($array as $key => $value) {
if (\is_array($value) && \count($value) > 1) {
$result += $this->flatterArray($value, $prefix.$key.'.');
} else {
$result[$prefix.$key] = \is_array($value) ? current($value) : $value;
}
}
return $result;
}
}