Skip to content

Commit 5926145

Browse files
committed
add array_split
1 parent fe8058c commit 5926145

File tree

4 files changed

+104
-5
lines changed

4 files changed

+104
-5
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.6.0 - 2015-10-28
6+
7+
### Added
8+
- array_split function
9+
510
## 1.5.0 - 2015-06-29
611

712
### Added

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,19 @@ function array_keys_exist($needles, array $haystack)
9595
function array_split_filter(array $array, callable $callback)
9696
```
9797

98+
### array_split
99+
```php
100+
/**
101+
* Split an array in the given amount of pieces.
102+
*
103+
* @param array $array
104+
* @param int $numberOfPieces
105+
* @param bool $preserveKeys
106+
* @return array
107+
*/
108+
function array_split(array $array, $numberOfPieces = 2, $preserveKeys = false)
109+
```
110+
98111
## Change log
99112

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

src/array_functions.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Get a random value from an array.
77
*
88
* @param array $array
9-
* @param int $numReq The amount of values to return
9+
* @param int $numReq The amount of values to return
1010
*
1111
* @return mixed
1212
*/
@@ -50,7 +50,7 @@ function array_rand_weighted(array $array)
5050
* Determine if all given needles are present in the haystack.
5151
*
5252
* @param array|string $needles
53-
* @param array $haystack
53+
* @param array $haystack
5454
*
5555
* @return bool
5656
*/
@@ -67,7 +67,7 @@ function values_in_array($needles, array $haystack)
6767
* Determine if all given needles are present in the haystack as array keys.
6868
*
6969
* @param array|string $needles
70-
* @param array $haystack
70+
* @param array $haystack
7171
*
7272
* @return bool
7373
*/
@@ -89,7 +89,7 @@ function array_keys_exist($needles, array $haystack)
8989
*
9090
* Array keys are preserved.
9191
*
92-
* @param array $array
92+
* @param array $array
9393
* @param callable $callback
9494
*
9595
* @return array
@@ -98,9 +98,29 @@ function array_split_filter(array $array, callable $callback)
9898
{
9999
$passesFilter = array_filter($array, $callback);
100100

101-
$negatedCallback = function ($item) use ($callback) { return ! $callback($item); };
101+
$negatedCallback = function ($item) use ($callback) { return !$callback($item); };
102102

103103
$doesNotPassFilter = array_filter($array, $negatedCallback);
104104

105105
return [$passesFilter, $doesNotPassFilter];
106106
}
107+
108+
/**
109+
* Split an array in the given amount of pieces.
110+
*
111+
* @param array $array
112+
* @param int $numberOfPieces
113+
* @param bool $preserveKeys
114+
*
115+
* @return array
116+
*/
117+
function array_split(array $array, $numberOfPieces = 2, $preserveKeys = false)
118+
{
119+
if (count($array) === 0) {
120+
return [];
121+
}
122+
123+
$splitSize = ceil(count($array) / $numberOfPieces);
124+
125+
return array_chunk($array, $splitSize, $preserveKeys);
126+
}

tests/ArraySplitTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace Spatie\Test;
4+
5+
use function spatie\array_split;
6+
7+
class ArraySplitTest extends \PHPUnit_Framework_TestCase
8+
{
9+
/**
10+
* @test
11+
*/
12+
public function it_can_handle_an_empty_array()
13+
{
14+
$this->assertSame(array_split([]), []);
15+
}
16+
17+
/**
18+
* @test
19+
*/
20+
public function it_splits_an_array_in_two_by_default()
21+
{
22+
$this->assertSame(array_split(['a', 'b']), [['a'], ['b']]);
23+
}
24+
25+
/**
26+
* @test
27+
*/
28+
public function it_splits_an_array_in_two_by_default_while_preserving_keys()
29+
{
30+
$this->assertSame(array_split(['a' => 1, 'b' => 2], 2, true), [['a' => 1], ['b' => 2]]);
31+
}
32+
33+
/**
34+
* @dataProvider arrayProvider
35+
*
36+
* @test
37+
*
38+
* @param $array
39+
* @param $splitIntoNumber
40+
* @param $expectedArray
41+
*/
42+
public function it_can_split_an_array_in_equal_pieces($array, $splitIntoNumber, $expectedArray)
43+
{
44+
$this->assertSame($expectedArray, array_split($array, $splitIntoNumber));
45+
}
46+
47+
public function arrayProvider()
48+
{
49+
return [
50+
[
51+
['a', 'b', 'c', 'd'], 2, [['a', 'b'], ['c', 'd']],
52+
],
53+
[
54+
['a', 'b', 'c', 'd', 'e'], 2, [['a', 'b', 'c'], ['d', 'e']],
55+
],
56+
[
57+
['a', 'b', 'c', 'd', 'e'], 3, [['a', 'b'], ['c', 'd'], ['e']],
58+
],
59+
];
60+
}
61+
}

0 commit comments

Comments
 (0)