Skip to content

Commit d7caf90

Browse files
committed
Fix 'bug' in array_merge_recursive
1 parent afb8305 commit d7caf90

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

src/Twig/Runtime.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function resources(Environment $twig, array $context, ProductInterface $p
5858
}
5959
}
6060

61-
$variants['optionReferences'] = array_merge_recursive(...$optionReferences);
61+
$variants['optionReferences'] = self::merge(...$optionReferences);
6262

6363
$flags = \JSON_THROW_ON_ERROR | \JSON_FORCE_OBJECT;
6464
if ($this->debug) {
@@ -102,4 +102,28 @@ private static function permutate(array $keys, string $finalValue): array
102102

103103
return $result;
104104
}
105+
106+
private static function merge(array ...$arrays): array
107+
{
108+
$merged = [];
109+
110+
foreach ($arrays as $array) {
111+
/** @var mixed $value */
112+
foreach ($array as $key => $value) {
113+
// This is different from array_merge_recursive because integer keys are appended in that
114+
if (isset($merged[$key])) {
115+
if (is_array($merged[$key]) && is_array($value)) {
116+
$merged[$key] = self::merge($merged[$key], $value);
117+
} else {
118+
$merged[$key] = [$merged[$key], $value];
119+
}
120+
} else {
121+
/** @psalm-suppress MixedAssignment */
122+
$merged[$key] = $value;
123+
}
124+
}
125+
}
126+
127+
return $merged;
128+
}
105129
}

0 commit comments

Comments
 (0)