Skip to content

Commit 82e9050

Browse files
committed
Merge pull request #5 from sebastiandedeyne/master
Added array_flatten
2 parents d911189 + 56fd74b commit 82e9050

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed

CHANGELOG.md

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

33
All Notable changes to `array-functions` will be documented in this file
44

5+
## 1.8.0 - 2016-03-08
6+
7+
### Added
8+
- array_flatten function
9+
510
## 1.7.0 - 2015-11-18
611

712
### Added

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,20 @@ function array_split(array $array, $numberOfPieces = 2, $preserveKeys = false)
119119
function array_merge_values(array ... $arrays)
120120
```
121121

122+
### array_flatten
123+
```php
124+
/**
125+
* Flatten an array of arrays. The `$levels` parameter specifies how deep you want to
126+
* recurse in the array. If `$levels` is -1, the function will recurse infinitely.
127+
*
128+
* @param array $array
129+
* @param int $levels
130+
*
131+
* @return array
132+
*/
133+
function array_flatten(array $array, $levels = -1)
134+
```
135+
122136
## Change log
123137

124138
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

src/array_functions.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,34 @@ function array_merge_values(array ...$arrays)
140140

141141
return array_values(array_unique($allValues));
142142
}
143+
144+
/**
145+
* Flatten an array of arrays. The `$levels` parameter specifies how deep you want to
146+
* recurse in the array. If `$levels` is -1, the function will recurse infinitely.
147+
*
148+
* @param array $array
149+
* @param int $levels
150+
*
151+
* @return array
152+
*/
153+
function array_flatten(array $array, $levels = -1)
154+
{
155+
if ($levels === 0) {
156+
return $array;
157+
}
158+
159+
$flattened = [];
160+
161+
if ($levels !== -1) {
162+
--$levels;
163+
}
164+
165+
foreach ($array as $element) {
166+
$flattened = array_merge(
167+
$flattened,
168+
is_array($element) ? array_flatten($element, $levels) : [$element]
169+
);
170+
}
171+
172+
return $flattened;
173+
}

tests/ArrayFlattenTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace Spatie\Test;
4+
5+
use function spatie\array_flatten;
6+
7+
class ArrayFlattenTest extends \PHPUnit_Framework_TestCase
8+
{
9+
/**
10+
* @test
11+
*/
12+
public function it_flattens_an_array()
13+
{
14+
$this->assertEquals(
15+
['a', 'b'],
16+
array_flatten(['a', ['b']])
17+
);
18+
}
19+
20+
/**
21+
* @test
22+
*/
23+
public function it_recursively_flattens_an_array()
24+
{
25+
$this->assertEquals(
26+
['a', 'b', 'c', 'd', 'e'],
27+
array_flatten(['a', ['b', ['c'], 'd'], 'e'])
28+
);
29+
}
30+
31+
/**
32+
* @test
33+
*/
34+
public function it_recursively_flattens_an_array_to_a_certain_level()
35+
{
36+
$this->assertEquals(
37+
['a', 'b', ['c'], 'd', 'e'],
38+
array_flatten(['a', ['b', ['c'], 'd'], 'e'], 1)
39+
);
40+
}
41+
42+
/**
43+
* @test
44+
*/
45+
public function it_doesnt_flatten_if_the_amount_of_levels_is_0()
46+
{
47+
$this->assertEquals(
48+
['a', ['b', ['c'], 'd'], 'e'],
49+
array_flatten(['a', ['b', ['c'], 'd'], 'e'], 0)
50+
);
51+
}
52+
}

0 commit comments

Comments
 (0)