Fully flatten an array (#2956)
* Fully flatten an array * Provide test coverage for CONCAT combined with INDEX/MATCH
This commit is contained in:
parent
b8456105dd
commit
e460c82606
|
|
@ -25,7 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
|
|||
|
||||
### Fixed
|
||||
|
||||
- Nothing
|
||||
- Fully flatten an array [Issue #2955](https://github.com/PHPOffice/PhpSpreadsheet/issues/2955) [PR #2956](https://github.com/PHPOffice/PhpSpreadsheet/pull/2956)
|
||||
|
||||
## 1.24.1 - 2022-07-18
|
||||
|
||||
|
|
|
|||
|
|
@ -573,24 +573,20 @@ class Functions
|
|||
return (array) $array;
|
||||
}
|
||||
|
||||
$arrayValues = [];
|
||||
foreach ($array as $value) {
|
||||
$flattened = [];
|
||||
$stack = array_values($array);
|
||||
|
||||
while ($stack) {
|
||||
$value = array_shift($stack);
|
||||
|
||||
if (is_array($value)) {
|
||||
foreach ($value as $val) {
|
||||
if (is_array($val)) {
|
||||
foreach ($val as $v) {
|
||||
$arrayValues[] = $v;
|
||||
}
|
||||
} else {
|
||||
$arrayValues[] = $val;
|
||||
}
|
||||
}
|
||||
array_unshift($stack, ...array_values($value));
|
||||
} else {
|
||||
$arrayValues[] = $value;
|
||||
$flattened[] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return $arrayValues;
|
||||
return $flattened;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheetTests\Calculation;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class ArrayTest extends TestCase
|
||||
{
|
||||
public function testMultiDimensionalArrayIsFlattened(): void
|
||||
{
|
||||
$array = [
|
||||
0 => [
|
||||
0 => [
|
||||
32 => [
|
||||
'B' => 'PHP',
|
||||
],
|
||||
],
|
||||
],
|
||||
1 => [
|
||||
0 => [
|
||||
32 => [
|
||||
'C' => 'Spreadsheet',
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$values = Functions::flattenArray($array);
|
||||
|
||||
self::assertIsNotArray($values[0]);
|
||||
self::assertIsNotArray($values[1]);
|
||||
}
|
||||
}
|
||||
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
|
||||
class ConcatenateTest extends AllSetupTeardown
|
||||
{
|
||||
/**
|
||||
|
|
@ -30,4 +32,27 @@ class ConcatenateTest extends AllSetupTeardown
|
|||
{
|
||||
return require 'tests/data/Calculation/TextData/CONCATENATE.php';
|
||||
}
|
||||
|
||||
public function testConcatenateWithIndexMatch(): void
|
||||
{
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$sheet1 = $spreadsheet->getActiveSheet();
|
||||
$sheet1->setTitle('Formula');
|
||||
$sheet1->fromArray(
|
||||
[
|
||||
['Number', 'Formula'],
|
||||
['52101293', '=CONCAT(INDEX(Lookup!$B$2, MATCH($A2, Lookup!$A$2, 0)))'],
|
||||
]
|
||||
);
|
||||
$sheet2 = $spreadsheet->createSheet();
|
||||
$sheet2->setTitle('Lookup');
|
||||
$sheet2->fromArray(
|
||||
[
|
||||
['Lookup', 'Match'],
|
||||
['52101293', 'PHP'],
|
||||
]
|
||||
);
|
||||
self::assertSame('PHP', $sheet1->getCell('B2')->getCalculatedValue());
|
||||
$spreadsheet->disconnectWorksheets();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue