Skip to content

Commit

Permalink
Added Arr::pluck and ArrDots::pluck switch combines column and collapse
Browse files Browse the repository at this point in the history
  • Loading branch information
pdscopes committed May 16, 2018
1 parent 199678f commit 9055785
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,9 @@ public static function unique($array, $flag = SORT_STRING)
* @return array
* @see \array_column()
*/
public static function column($array, $columns, $indexKey = null)
public static function column(array $array = null, $columns, $indexKey = null)
{
$array = $array ?? [];
$array = (array) $array;
$columns = (array) $columns;
$last = array_pop($columns);
foreach ($columns as $column) {
Expand All @@ -175,6 +175,30 @@ public static function column($array, $columns, $indexKey = null)
return array_column($array, $last, $indexKey);
}

/**
* Pluck the values from a single column in `$array`.
* If an element in `$columns` is `null` then collapse the `$array`
* An array of columns can be provided to chain call column.
*
* @param array $array
* @param string|array $columns
* @return array
* @see \array_column()
*/
public static function pluck(array $array = null, $columns)
{
$array = (array) $array;
$columns = (array) $columns;
foreach ($columns as $column) {
if ($column !== null) {
$array = array_column($array, $column);
} else {
$array = array_merge(...$array);
}
}
return $array;
}

/**
* @param ArrayAccess|array $array
* @param string|int $key
Expand Down
20 changes: 20 additions & 0 deletions src/ArrDots.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,26 @@ public static function column($array, $dots, $indexKey = null)
return Arr::column($array, explode('.', $dots), $indexKey);
}

/**
* Pluck the values from a single column in `$array`.
* A `$wildcard` can be set to collapse the array at that point.
* An array of columns can be provided to chain call column.
*
* @param array $array
* @param string $dots
* @param string $wildcard
* @return array
* @see \MadeSimple\Arrays\Arr::column()
* @see \array_column()
*/
public static function pluck($array, $dots, $wildcard = null)
{
$dots = array_map(function ($i) use ($wildcard) {
return $i === $wildcard ? null : $i;
}, explode('.', $dots));
return Arr::pluck($array, $dots);
}

/**
* @param ArrayAccess|array $array
* @param array|string $keys
Expand Down
52 changes: 52 additions & 0 deletions tests/Unit/ArrDotsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,58 @@ public function testColumn()
$this->assertEquals($partialArray3, ArrDots::column($completeArray, 'three.0.epsilon'));
}

public function testPluck()
{
$completeArray = [
[
'one' => '1st one',
'two' => ['alpha' => '1st two alpha', 'beta' => '1st two beta', 'id' => '1A'],
'three' => [['gamma' => '1st gamma', 'epsilon' => '1st epsilon']],
'four' => [['gamma' => '1st gamma 1st'], ['gamma' => '1st gamma 2nd']],
],
[
'one' => '2nd one',
'two' => ['alpha' => '2nd two alpha', 'beta' => '2nd two beta', 'id' => '2B'],
'three' => [['gamma' => '2nd gamma', 'epsilon' => '2nd epsilon']],
'four' => [['gamma' => '2nd gamma 1st'], ['gamma' => '2nd gamma 2nd']],
],
[
'one' => '3rd one',
'two' => ['alpha' => '3rd two alpha', 'beta' => '3rd two beta', 'id' => '3C'],
'three' => [['gamma' => '3rd gamma', 'epsilon' => '3rd epsilon']],
'four' => [['gamma' => '3rd gamma 1st'], ['gamma' => '3rd gamma 2nd']],
],
];
$partialArray1 = [
'1st one',
'2nd one',
'3rd one',
];
$partialArray2 = [
'1st two alpha',
'2nd two alpha',
'3rd two alpha',
];
$partialArray3 = [
'1st epsilon',
'2nd epsilon',
'3rd epsilon',
];
$partialArray4 = [
'1st gamma 1st',
'1st gamma 2nd',
'2nd gamma 1st',
'2nd gamma 2nd',
'3rd gamma 1st',
'3rd gamma 2nd',
];

$this->assertEquals($partialArray1, ArrDots::pluck($completeArray, 'one'));
$this->assertEquals($partialArray2, ArrDots::pluck($completeArray, 'two.alpha'));
$this->assertEquals($partialArray3, ArrDots::pluck($completeArray, 'three.0.epsilon'));
$this->assertEquals($partialArray4, ArrDots::pluck($completeArray, 'four.*.gamma', '*'));
}

public function testRemove()
{
$completeArray = [
Expand Down
52 changes: 52 additions & 0 deletions tests/Unit/ArrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,58 @@ public function testColumn()
$this->assertEquals($partialArray3, Arr::column($completeArray, ['three', 0, 'epsilon']));
}

public function testPluck()
{
$completeArray = [
[
'one' => '1st one',
'two' => ['alpha' => '1st two alpha', 'beta' => '1st two beta', 'id' => '1A'],
'three' => [['gamma' => '1st gamma', 'epsilon' => '1st epsilon']],
'four' => [['gamma' => '1st gamma 1st'], ['gamma' => '1st gamma 2nd']],
],
[
'one' => '2nd one',
'two' => ['alpha' => '2nd two alpha', 'beta' => '2nd two beta', 'id' => '2B'],
'three' => [['gamma' => '2nd gamma', 'epsilon' => '2nd epsilon']],
'four' => [['gamma' => '2nd gamma 1st'], ['gamma' => '2nd gamma 2nd']],
],
[
'one' => '3rd one',
'two' => ['alpha' => '3rd two alpha', 'beta' => '3rd two beta', 'id' => '3C'],
'three' => [['gamma' => '3rd gamma', 'epsilon' => '3rd epsilon']],
'four' => [['gamma' => '3rd gamma 1st'], ['gamma' => '3rd gamma 2nd']],
],
];
$partialArray1 = [
'1st one',
'2nd one',
'3rd one',
];
$partialArray2 = [
'1st two alpha',
'2nd two alpha',
'3rd two alpha',
];
$partialArray3 = [
'1st epsilon',
'2nd epsilon',
'3rd epsilon',
];
$partialArray4 = [
'1st gamma 1st',
'1st gamma 2nd',
'2nd gamma 1st',
'2nd gamma 2nd',
'3rd gamma 1st',
'3rd gamma 2nd',
];

$this->assertEquals($partialArray1, Arr::pluck($completeArray, 'one'));
$this->assertEquals($partialArray2, Arr::pluck($completeArray, ['two', 'alpha']));
$this->assertEquals($partialArray3, Arr::pluck($completeArray, ['three', 0, 'epsilon']));
$this->assertEquals($partialArray4, Arr::pluck($completeArray, ['four', null, 'gamma']));
}

public function testExists()
{
$array = [
Expand Down

0 comments on commit 9055785

Please sign in to comment.