Skip to content

Commit f10278e

Browse files
committed
Added Arr::collapse, Arr::column, and ArrDots::column
1 parent 9d7be92 commit f10278e

File tree

4 files changed

+176
-14
lines changed

4 files changed

+176
-14
lines changed

src/Arr.php

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,31 @@ public static function isAssoc(array $array)
4040
/**
4141
* Divide an array into two arrays. One with keys and the other with values.
4242
*
43-
* @param array $array
43+
* @param Arrayable|array $array
4444
*
4545
* @return array [array, array]
4646
*/
4747
public static function divide($array)
4848
{
49+
if ($array instanceof ArrayAble) {
50+
$array = $array->toArray();
51+
}
4952
return [array_keys($array), array_values($array)];
5053
}
5154

5255
/**
5356
* Flatten a multi-dimensional array into a single level.
5457
*
55-
* @param array $array
56-
* @param int $depth
58+
* @param Arrayable|array $array
59+
* @param int $depth
5760
*
5861
* @return array
5962
*/
6063
public static function flatten($array, $depth = INF)
6164
{
65+
if ($array instanceof ArrayAble) {
66+
$array = $array->toArray();
67+
}
6268
return array_reduce($array, function ($result, $item) use ($depth) {
6369
if (!is_array($item)) {
6470
return array_merge($result, [$item]);
@@ -70,58 +76,105 @@ public static function flatten($array, $depth = INF)
7076
}, []);
7177
}
7278

79+
/**
80+
* Merge all elements of $array into a single array.
81+
*
82+
* @param ArrayAble|array $array
83+
* @return array
84+
*/
85+
public static function collapse($array)
86+
{
87+
if ($array instanceof ArrayAble) {
88+
$array = $array->toArray();
89+
}
90+
return array_merge(...$array);
91+
}
92+
7393
/**
7494
* Get a subset of the items from $array that only contains $keys.
7595
*
76-
* @param array $array
96+
* @param Arrayable|array $array
7797
* @param int|string|array $keys
7898
*
7999
* @return array
80100
*/
81101
public static function only($array, $keys)
82102
{
103+
if ($array instanceof ArrayAble) {
104+
$array = $array->toArray();
105+
}
83106
return array_intersect_key($array, array_flip((array) $keys));
84107
}
85108

86109
/**
87110
* Get a subset of the items from $array that contains all keys except $keys.
88111
*
89-
* @param array $array
112+
* @param ArrayAble|array $array
90113
* @param int|string|array $keys
91114
*
92115
* @return array
93116
*/
94117
public static function except($array, $keys)
95118
{
119+
if ($array instanceof ArrayAble) {
120+
$array = $array->toArray();
121+
}
96122
return array_diff_key($array, array_flip((array) $keys));
97123
}
98124

99125
/**
100126
* Get a subset of items from $array that pass $callback test.
101127
*
102-
* @param ArrayAccess|array $array
103-
* @param callable $callback
128+
* @param ArrayAble|array $array
129+
* @param callable $callback
104130
*
105131
* @return array
106132
*/
107133
public static function filter($array, callable $callback)
108134
{
135+
if ($array instanceof ArrayAble) {
136+
$array = $array->toArray();
137+
}
109138
return array_filter($array, $callback, ARRAY_FILTER_USE_BOTH);
110139
}
111140

112141
/**
113142
* Get a subset of unique items from $array.
114143
*
115-
* @param ArrayAccess|array $array
116-
* @param int $flag
144+
* @param ArrayAble|array $array
145+
* @param int $flag
117146
*
118147
* @return array
119148
*/
120149
public static function unique($array, $flag = SORT_STRING)
121150
{
151+
if ($array instanceof ArrayAble) {
152+
$array = $array->toArray();
153+
}
122154
return array_unique($array, $flag);
123155
}
124156

157+
/**
158+
* Get the values from a single column in $array.
159+
* An array of columns can be provided to chain call column.
160+
*
161+
* @param array $array
162+
* @param string|array $columns
163+
* @param string $indexKey Only applied to the last column
164+
* @return array
165+
* @see \array_column()
166+
*/
167+
public static function column($array, $columns, $indexKey = null)
168+
{
169+
$array = $array ?? [];
170+
$columns = (array) $columns;
171+
$last = array_pop($columns);
172+
foreach ($columns as $column) {
173+
$array = array_column($array, $column);
174+
}
175+
return array_column($array, $last, $indexKey);
176+
}
177+
125178
/**
126179
* @param ArrayAccess|array $array
127180
* @param string|int $key

src/ArrDots.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ public static function implode($array, $prepend = '')
2525

2626
foreach ($array as $key => $value) {
2727
if (is_array($value) && !empty($value)) {
28-
$results = array_merge($results, static::implode($value, $prepend.$key.'.'));
29-
} else {
30-
$results[$prepend.$key] = $value;
28+
$results = array_merge($results, static::implode($value, $prepend . $key . '.'));
29+
}
30+
else {
31+
$results[$prepend . $key] = $value;
3132
}
3233
}
3334

@@ -52,6 +53,22 @@ public static function explode($array)
5253
return $results;
5354
}
5455

56+
/**
57+
* Get the values from a single column in $array.
58+
* An array of columns can be provided to chain call column.
59+
*
60+
* @param array $array
61+
* @param string $dots
62+
* @param string $indexKey Only applied to the last column
63+
* @return array
64+
* @see \MadeSimple\Arrays\Arr::column()
65+
* @see \array_column()
66+
*/
67+
public static function column($array, $dots, $indexKey = null)
68+
{
69+
return Arr::column($array, explode('.', $dots), $indexKey);
70+
}
71+
5572
/**
5673
* @param ArrayAccess|array $array
5774
* @param array|string $keys

tests/Unit/ArrDotsTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,46 @@ public function testExplode()
4747
$this->assertEquals($multiDimensionalArray, ArrDots::explode($implodedArray));
4848
}
4949

50+
public function testColumn()
51+
{
52+
$completeArray = [
53+
[
54+
'one' => '1st one',
55+
'two' => ['alpha' => '1st two alpha', 'beta' => '1st two beta', 'id' => '1A'],
56+
'three' => [['gamma' => '1st gamma', 'epsilon' => '1st epsilon']]
57+
],
58+
[
59+
'one' => '2nd one',
60+
'two' => ['alpha' => '2nd two alpha', 'beta' => '2nd two beta', 'id' => '2B'],
61+
'three' => [['gamma' => '2nd gamma', 'epsilon' => '2nd epsilon']]
62+
],
63+
[
64+
'one' => '3rd one',
65+
'two' => ['alpha' => '3rd two alpha', 'beta' => '3rd two beta', 'id' => '3C'],
66+
'three' => [['gamma' => '3rd gamma', 'epsilon' => '3rd epsilon']]
67+
],
68+
];
69+
$partialArray1 = [
70+
'1st one',
71+
'2nd one',
72+
'3rd one',
73+
];
74+
$partialArray2 = [
75+
'1A' => '1st two alpha',
76+
'2B' => '2nd two alpha',
77+
'3C' => '3rd two alpha',
78+
];
79+
$partialArray3 = [
80+
'1st epsilon',
81+
'2nd epsilon',
82+
'3rd epsilon',
83+
];
84+
85+
$this->assertEquals($partialArray1, ArrDots::column($completeArray, 'one'));
86+
$this->assertEquals($partialArray2, ArrDots::column($completeArray, 'two.alpha', 'id'));
87+
$this->assertEquals($partialArray3, ArrDots::column($completeArray, 'three.0.epsilon'));
88+
}
89+
5090
public function testRemove()
5191
{
5292
$completeArray = [

tests/Unit/ArrTest.php

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@ public function testFlatten()
5959
$this->assertEquals($flattenedArray, Arr::flatten($multiDimensionalArray));
6060
}
6161

62+
public function testCollapse()
63+
{
64+
$original = [
65+
['alpha'],
66+
['beta'],
67+
['gamma'],
68+
];
69+
$collapsed = ['alpha', 'beta', 'gamma'];
70+
71+
$this->assertEquals($collapsed, Arr::collapse($original));
72+
}
73+
6274
public function testOnly()
6375
{
6476
$completeArray = [
@@ -126,17 +138,57 @@ public function testFilter()
126138
}));
127139
}
128140

129-
public function unique()
141+
public function testUnique()
130142
{
131143
$completeArray = [3,3, 5,5,5,5, 7,7,7,7,7,7, 9,9,9,9,9,9,9,9];
132-
$filteredArray = [3, 5, 7, 9];
144+
$filteredArray = [0 => 3, 2 => 5, 6 => 7, 12 => 9];
133145

134146
$this->assertEquals($filteredArray, Arr::unique($completeArray));
135147

136148
$completeDots = new Dots($completeArray);
137149
$this->assertEquals($filteredArray, Arr::unique($completeDots));
138150
}
139151

152+
public function testColumn()
153+
{
154+
$completeArray = [
155+
[
156+
'one' => '1st one',
157+
'two' => ['alpha' => '1st two alpha', 'beta' => '1st two beta', 'id' => '1A'],
158+
'three' => [['gamma' => '1st gamma', 'epsilon' => '1st epsilon']]
159+
],
160+
[
161+
'one' => '2nd one',
162+
'two' => ['alpha' => '2nd two alpha', 'beta' => '2nd two beta', 'id' => '2B'],
163+
'three' => [['gamma' => '2nd gamma', 'epsilon' => '2nd epsilon']]
164+
],
165+
[
166+
'one' => '3rd one',
167+
'two' => ['alpha' => '3rd two alpha', 'beta' => '3rd two beta', 'id' => '3C'],
168+
'three' => [['gamma' => '3rd gamma', 'epsilon' => '3rd epsilon']]
169+
],
170+
];
171+
$partialArray1 = [
172+
'1st one',
173+
'2nd one',
174+
'3rd one',
175+
];
176+
$partialArray2 = [
177+
'1A' => '1st two alpha',
178+
'2B' => '2nd two alpha',
179+
'3C' => '3rd two alpha',
180+
];
181+
$partialArray3 = [
182+
'1st epsilon',
183+
'2nd epsilon',
184+
'3rd epsilon',
185+
];
186+
187+
$this->assertEquals($partialArray1, Arr::column($completeArray, 'one'));
188+
$this->assertEquals($partialArray2, Arr::column($completeArray, ['two', 'alpha'], 'id'));
189+
$this->assertEquals($partialArray3, Arr::column($completeArray, ['three', 0, 'epsilon']));
190+
}
191+
140192
public function testExists()
141193
{
142194
$array = [

0 commit comments

Comments
 (0)