Skip to content

Instantly share code, notes, and snippets.

@AustinW
Created January 20, 2026 22:21
Show Gist options
  • Select an option

  • Save AustinW/b808f201b0cb49bbbd89b4a1a611f25f to your computer and use it in GitHub Desktop.

Select an option

Save AustinW/b808f201b0cb49bbbd89b4a1a611f25f to your computer and use it in GitHub Desktop.
Array Flatten
<?php
/**
* Recursively flattens an array of arbitrarily nested arrays of integers.
*
* @param array $array The nested array to flatten
* @return array A flat array of integers
*/
function flatten(array $array): array
{
$result = [];
foreach ($array as $item) {
if (is_array($item)) {
$result = array_merge($result, flatten($item));
} else {
$result[] = $item;
}
}
return $result;
}
<?php
use PHPUnit\Framework\TestCase;
class ArrayFlattenTest extends TestCase
{
public function test_it_flattens_the_example_case(): void
{
$this->assertEquals(
[1, 2, 3, 4],
flatten([[1, 2, [3]], 4])
);
}
public function test_it_handles_empty_array(): void
{
$this->assertEquals([], flatten([]));
}
public function test_it_handles_already_flat_array(): void
{
$this->assertEquals(
[1, 2, 3, 4, 5],
flatten([1, 2, 3, 4, 5])
);
}
public function test_it_handles_single_element(): void
{
$this->assertEquals([42], flatten([42]));
}
public function test_it_handles_single_nested_element(): void
{
$this->assertEquals([42], flatten([[42]]));
}
public function test_it_flattens_deeply_nested_arrays(): void
{
$this->assertEquals(
[1, 2, 3, 4, 5, 6],
flatten([1, [2, [3, [4, [5, [6]]]]]])
);
}
public function test_it_handles_empty_nested_arrays(): void
{
$this->assertEquals(
[1, 2, 3],
flatten([1, [], [2, []], 3, [[]]])
);
}
public function test_it_handles_completely_nested_structure(): void
{
$this->assertEquals(
[1, 2, 3, 4],
flatten([[[1]], [[2]], [[3]], [[4]]])
);
}
public function test_it_preserves_zero_values(): void
{
$this->assertEquals(
[0, 1, 0, 2],
flatten([[0, 1], [0, [2]]])
);
}
public function test_it_handles_negative_integers(): void
{
$this->assertEquals(
[-5, -1, 0, 1, 5],
flatten([[-5, -1], [0, [1, [5]]]])
);
}
public function test_it_handles_mixed_nesting_depths(): void
{
$this->assertEquals(
[1, 2, 3, 4, 5, 6, 7, 8],
flatten([1, [2, 3], [[4, 5]], [[[6]]], 7, [8]])
);
}
public function test_it_handles_large_flat_array(): void
{
$input = range(1, 1000);
$this->assertEquals($input, flatten($input));
}
public function test_it_handles_large_nested_array(): void
{
$nested = [];
for ($i = 1; $i <= 100; $i++) {
$nested[] = [$i, [$i + 100]];
}
$expected = [];
for ($i = 1; $i <= 100; $i++) {
$expected[] = $i;
$expected[] = $i + 100;
}
$this->assertEquals($expected, flatten($nested));
}
public function test_it_preserves_order(): void
{
$this->assertEquals(
[1, 2, 3, 4, 5, 6],
flatten([[1], [2], [3], [4], [5], [6]])
);
$this->assertNotEquals(
[6, 5, 4, 3, 2, 1],
flatten([[1], [2], [3], [4], [5], [6]])
);
}
public function test_it_handles_arrays_with_only_empty_arrays(): void
{
$this->assertEquals([], flatten([[], [[]], [[[]]]]));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment