Skip to content

Commit e460c82

Browse files
authored
Fully flatten an array (#2956)
* Fully flatten an array * Provide test coverage for CONCAT combined with INDEX/MATCH
1 parent b845610 commit e460c82

File tree

4 files changed

+69
-14
lines changed

4 files changed

+69
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
2525

2626
### Fixed
2727

28-
- Nothing
28+
- Fully flatten an array [Issue #2955](https://github.com/PHPOffice/PhpSpreadsheet/issues/2955) [PR #2956](https://github.com/PHPOffice/PhpSpreadsheet/pull/2956)
2929

3030
## 1.24.1 - 2022-07-18
3131

src/PhpSpreadsheet/Calculation/Functions.php

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -573,24 +573,20 @@ public static function flattenArray($array)
573573
return (array) $array;
574574
}
575575

576-
$arrayValues = [];
577-
foreach ($array as $value) {
576+
$flattened = [];
577+
$stack = array_values($array);
578+
579+
while ($stack) {
580+
$value = array_shift($stack);
581+
578582
if (is_array($value)) {
579-
foreach ($value as $val) {
580-
if (is_array($val)) {
581-
foreach ($val as $v) {
582-
$arrayValues[] = $v;
583-
}
584-
} else {
585-
$arrayValues[] = $val;
586-
}
587-
}
583+
array_unshift($stack, ...array_values($value));
588584
} else {
589-
$arrayValues[] = $value;
585+
$flattened[] = $value;
590586
}
591587
}
592588

593-
return $arrayValues;
589+
return $flattened;
594590
}
595591

596592
/**
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheetTests\Calculation;
4+
5+
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class ArrayTest extends TestCase
9+
{
10+
public function testMultiDimensionalArrayIsFlattened(): void
11+
{
12+
$array = [
13+
0 => [
14+
0 => [
15+
32 => [
16+
'B' => 'PHP',
17+
],
18+
],
19+
],
20+
1 => [
21+
0 => [
22+
32 => [
23+
'C' => 'Spreadsheet',
24+
],
25+
],
26+
],
27+
];
28+
29+
$values = Functions::flattenArray($array);
30+
31+
self::assertIsNotArray($values[0]);
32+
self::assertIsNotArray($values[1]);
33+
}
34+
}

tests/PhpSpreadsheetTests/Calculation/Functions/TextData/ConcatenateTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData;
44

5+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
6+
57
class ConcatenateTest extends AllSetupTeardown
68
{
79
/**
@@ -30,4 +32,27 @@ public function providerCONCATENATE(): array
3032
{
3133
return require 'tests/data/Calculation/TextData/CONCATENATE.php';
3234
}
35+
36+
public function testConcatenateWithIndexMatch(): void
37+
{
38+
$spreadsheet = new Spreadsheet();
39+
$sheet1 = $spreadsheet->getActiveSheet();
40+
$sheet1->setTitle('Formula');
41+
$sheet1->fromArray(
42+
[
43+
['Number', 'Formula'],
44+
['52101293', '=CONCAT(INDEX(Lookup!$B$2, MATCH($A2, Lookup!$A$2, 0)))'],
45+
]
46+
);
47+
$sheet2 = $spreadsheet->createSheet();
48+
$sheet2->setTitle('Lookup');
49+
$sheet2->fromArray(
50+
[
51+
['Lookup', 'Match'],
52+
['52101293', 'PHP'],
53+
]
54+
);
55+
self::assertSame('PHP', $sheet1->getCell('B2')->getCalculatedValue());
56+
$spreadsheet->disconnectWorksheets();
57+
}
3358
}

0 commit comments

Comments
 (0)