Skip to content

Commit 9055785

Browse files
committed
Added Arr::pluck and ArrDots::pluck switch combines column and collapse
1 parent 199678f commit 9055785

File tree

4 files changed

+150
-2
lines changed

4 files changed

+150
-2
lines changed

src/Arr.php

+26-2
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,9 @@ public static function unique($array, $flag = SORT_STRING)
164164
* @return array
165165
* @see \array_column()
166166
*/
167-
public static function column($array, $columns, $indexKey = null)
167+
public static function column(array $array = null, $columns, $indexKey = null)
168168
{
169-
$array = $array ?? [];
169+
$array = (array) $array;
170170
$columns = (array) $columns;
171171
$last = array_pop($columns);
172172
foreach ($columns as $column) {
@@ -175,6 +175,30 @@ public static function column($array, $columns, $indexKey = null)
175175
return array_column($array, $last, $indexKey);
176176
}
177177

178+
/**
179+
* Pluck the values from a single column in `$array`.
180+
* If an element in `$columns` is `null` then collapse the `$array`
181+
* An array of columns can be provided to chain call column.
182+
*
183+
* @param array $array
184+
* @param string|array $columns
185+
* @return array
186+
* @see \array_column()
187+
*/
188+
public static function pluck(array $array = null, $columns)
189+
{
190+
$array = (array) $array;
191+
$columns = (array) $columns;
192+
foreach ($columns as $column) {
193+
if ($column !== null) {
194+
$array = array_column($array, $column);
195+
} else {
196+
$array = array_merge(...$array);
197+
}
198+
}
199+
return $array;
200+
}
201+
178202
/**
179203
* @param ArrayAccess|array $array
180204
* @param string|int $key

src/ArrDots.php

+20
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,26 @@ public static function column($array, $dots, $indexKey = null)
6969
return Arr::column($array, explode('.', $dots), $indexKey);
7070
}
7171

72+
/**
73+
* Pluck the values from a single column in `$array`.
74+
* A `$wildcard` can be set to collapse the array at that point.
75+
* An array of columns can be provided to chain call column.
76+
*
77+
* @param array $array
78+
* @param string $dots
79+
* @param string $wildcard
80+
* @return array
81+
* @see \MadeSimple\Arrays\Arr::column()
82+
* @see \array_column()
83+
*/
84+
public static function pluck($array, $dots, $wildcard = null)
85+
{
86+
$dots = array_map(function ($i) use ($wildcard) {
87+
return $i === $wildcard ? null : $i;
88+
}, explode('.', $dots));
89+
return Arr::pluck($array, $dots);
90+
}
91+
7292
/**
7393
* @param ArrayAccess|array $array
7494
* @param array|string $keys

tests/Unit/ArrDotsTest.php

+52
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,58 @@ public function testColumn()
8787
$this->assertEquals($partialArray3, ArrDots::column($completeArray, 'three.0.epsilon'));
8888
}
8989

90+
public function testPluck()
91+
{
92+
$completeArray = [
93+
[
94+
'one' => '1st one',
95+
'two' => ['alpha' => '1st two alpha', 'beta' => '1st two beta', 'id' => '1A'],
96+
'three' => [['gamma' => '1st gamma', 'epsilon' => '1st epsilon']],
97+
'four' => [['gamma' => '1st gamma 1st'], ['gamma' => '1st gamma 2nd']],
98+
],
99+
[
100+
'one' => '2nd one',
101+
'two' => ['alpha' => '2nd two alpha', 'beta' => '2nd two beta', 'id' => '2B'],
102+
'three' => [['gamma' => '2nd gamma', 'epsilon' => '2nd epsilon']],
103+
'four' => [['gamma' => '2nd gamma 1st'], ['gamma' => '2nd gamma 2nd']],
104+
],
105+
[
106+
'one' => '3rd one',
107+
'two' => ['alpha' => '3rd two alpha', 'beta' => '3rd two beta', 'id' => '3C'],
108+
'three' => [['gamma' => '3rd gamma', 'epsilon' => '3rd epsilon']],
109+
'four' => [['gamma' => '3rd gamma 1st'], ['gamma' => '3rd gamma 2nd']],
110+
],
111+
];
112+
$partialArray1 = [
113+
'1st one',
114+
'2nd one',
115+
'3rd one',
116+
];
117+
$partialArray2 = [
118+
'1st two alpha',
119+
'2nd two alpha',
120+
'3rd two alpha',
121+
];
122+
$partialArray3 = [
123+
'1st epsilon',
124+
'2nd epsilon',
125+
'3rd epsilon',
126+
];
127+
$partialArray4 = [
128+
'1st gamma 1st',
129+
'1st gamma 2nd',
130+
'2nd gamma 1st',
131+
'2nd gamma 2nd',
132+
'3rd gamma 1st',
133+
'3rd gamma 2nd',
134+
];
135+
136+
$this->assertEquals($partialArray1, ArrDots::pluck($completeArray, 'one'));
137+
$this->assertEquals($partialArray2, ArrDots::pluck($completeArray, 'two.alpha'));
138+
$this->assertEquals($partialArray3, ArrDots::pluck($completeArray, 'three.0.epsilon'));
139+
$this->assertEquals($partialArray4, ArrDots::pluck($completeArray, 'four.*.gamma', '*'));
140+
}
141+
90142
public function testRemove()
91143
{
92144
$completeArray = [

tests/Unit/ArrTest.php

+52
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,58 @@ public function testColumn()
189189
$this->assertEquals($partialArray3, Arr::column($completeArray, ['three', 0, 'epsilon']));
190190
}
191191

192+
public function testPluck()
193+
{
194+
$completeArray = [
195+
[
196+
'one' => '1st one',
197+
'two' => ['alpha' => '1st two alpha', 'beta' => '1st two beta', 'id' => '1A'],
198+
'three' => [['gamma' => '1st gamma', 'epsilon' => '1st epsilon']],
199+
'four' => [['gamma' => '1st gamma 1st'], ['gamma' => '1st gamma 2nd']],
200+
],
201+
[
202+
'one' => '2nd one',
203+
'two' => ['alpha' => '2nd two alpha', 'beta' => '2nd two beta', 'id' => '2B'],
204+
'three' => [['gamma' => '2nd gamma', 'epsilon' => '2nd epsilon']],
205+
'four' => [['gamma' => '2nd gamma 1st'], ['gamma' => '2nd gamma 2nd']],
206+
],
207+
[
208+
'one' => '3rd one',
209+
'two' => ['alpha' => '3rd two alpha', 'beta' => '3rd two beta', 'id' => '3C'],
210+
'three' => [['gamma' => '3rd gamma', 'epsilon' => '3rd epsilon']],
211+
'four' => [['gamma' => '3rd gamma 1st'], ['gamma' => '3rd gamma 2nd']],
212+
],
213+
];
214+
$partialArray1 = [
215+
'1st one',
216+
'2nd one',
217+
'3rd one',
218+
];
219+
$partialArray2 = [
220+
'1st two alpha',
221+
'2nd two alpha',
222+
'3rd two alpha',
223+
];
224+
$partialArray3 = [
225+
'1st epsilon',
226+
'2nd epsilon',
227+
'3rd epsilon',
228+
];
229+
$partialArray4 = [
230+
'1st gamma 1st',
231+
'1st gamma 2nd',
232+
'2nd gamma 1st',
233+
'2nd gamma 2nd',
234+
'3rd gamma 1st',
235+
'3rd gamma 2nd',
236+
];
237+
238+
$this->assertEquals($partialArray1, Arr::pluck($completeArray, 'one'));
239+
$this->assertEquals($partialArray2, Arr::pluck($completeArray, ['two', 'alpha']));
240+
$this->assertEquals($partialArray3, Arr::pluck($completeArray, ['three', 0, 'epsilon']));
241+
$this->assertEquals($partialArray4, Arr::pluck($completeArray, ['four', null, 'gamma']));
242+
}
243+
192244
public function testExists()
193245
{
194246
$array = [

0 commit comments

Comments
 (0)