Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
arrayflatterator.php
1<?php
2
4
6{
7 private array $processedKeys = [];
8
9 public function __construct(array $processedKeys = [])
10 {
11 $this->processedKeys = $processedKeys;
12 }
13
14 public function flatten(array $input): array
15 {
16 if (!empty($this->processedKeys))
17 {
18 return $this->flattenWithCheckKeys($input);
19 }
20
21 foreach ($input as $key => $value)
22 {
23 if (is_array($value))
24 {
25 $newItems = $this->getFlattenFields($value, $key);
26 foreach ($newItems as $newKey => $newValue)
27 {
28 $input[$newKey] = $newValue;
29 }
30 }
31 }
32
33 return $input;
34 }
35
36 private function flattenWithCheckKeys(array $input): array
37 {
38 foreach ($input as $key => $value)
39 {
40 if (is_array($value) && in_array($key, $this->processedKeys, true))
41 {
42 $newItems = $this->getFlattenFields($value, $key);
43 foreach ($newItems as $newKey => $newValue)
44 {
45 $input[$newKey] = $newValue;
46 }
47 }
48 }
49
50 return $input;
51 }
52
53 private function getFlattenFields(array $fields, string $prefix): array
54 {
55 $result = [];
56
57 foreach ($fields as $name => $value)
58 {
59 $key = "{$prefix}[{$name}]";
60 if (is_array($value))
61 {
62 array_push($result, ...$this->getFlattenFields($value, $key));
63 }
64 else
65 {
66 $result[$key] = $value;
67 }
68 }
69
70 return $result;
71 }
72}