Skip to content

Commit bbb69a9

Browse files
GrahamCampbellAlbert Boada
andcommitted
Fix flattening in arrays starting with null
Co-Authored-By: Albert Boada <[email protected]>
1 parent f3734e2 commit bbb69a9

File tree

5 files changed

+37
-2
lines changed

5 files changed

+37
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## 2.7.0 - UPCOMING
44

5+
* Fixed flattening in arrays starting with null
56
* Drop support for HHVM and PHP earlier than 7.2.5.
67
* Add support for PHP 8.1, 8.2, and 8.3.
78

src/TreeCompiler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ private function visit_flatten(array $node)
305305
->write('%s = [];', $merged)
306306
->write('foreach ($value as %s) {', $val)
307307
->indent()
308-
->write('if (is_array(%s) && isset(%s[0])) {', $val, $val)
308+
->write('if (is_array(%s) && array_key_exists(0, %s)) {', $val, $val)
309309
->indent()
310310
->write('%s = array_merge(%s, %s);', $merged, $merged, $val)
311311
->outdent()

src/TreeInterpreter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ private function dispatch(array $node, $value)
107107
$merged = [];
108108
foreach ($value as $values) {
109109
// Only merge up arrays lists and not hashes
110-
if (is_array($values) && isset($values[0])) {
110+
if (is_array($values) && array_key_exists(0, $values)) {
111111
$merged = array_merge($merged, $values);
112112
} elseif ($values !== $skipElement) {
113113
$merged[] = $values;

tests/compliance/indices.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,29 @@
297297
}
298298
]
299299
},
300+
{
301+
"given":
302+
{
303+
"foo": [
304+
{"bar": [null, null, 3, 5, 6, null]},
305+
{"bar": [1, 3, null, 5]}
306+
]
307+
},
308+
"cases": [
309+
{
310+
"expression": "foo[*].bar",
311+
"result": [[null, null, 3, 5, 6, null], [1, 3, null, 5]]
312+
},
313+
{
314+
"expression": "foo[*].bar[*]",
315+
"result": [[3, 5, 6], [1, 3, 5]]
316+
},
317+
{
318+
"expression": "foo[*].bar[]",
319+
"result": [3, 5, 6, 1, 3, 5]
320+
}
321+
]
322+
},
300323
{
301324
"given": {
302325
"string": "string",

tests/compliance/wildcard.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,5 +456,16 @@
456456
"result": [0, 0]
457457
}
458458
]
459+
},
460+
{
461+
"given": {
462+
"foo": [null, 1, null, 2, null]
463+
},
464+
"cases": [
465+
{
466+
"expression": "foo[*]",
467+
"result": [1, 2]
468+
}
469+
]
459470
}
460471
]

0 commit comments

Comments
 (0)