Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Illuminate/Support/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,18 @@ public static function flatten($array, $depth = INF)
return $result;
}

/**
* Map an array and flatten the result by a single level.
*
* @param array $array
* @param callable $callback
* @return array
*/
public static function flatMap($array, callable $callback)
{
return Collection::make($array)->flatMap($callback)->all();
}

/**
* Remove one or many array items from a given array using "dot" notation.
*
Expand Down
12 changes: 12 additions & 0 deletions tests/Support/SupportArrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,18 @@ public function testFlattenWithDepth()
$this->assertEquals(['#foo', '#bar', ['#baz'], '#zap'], Arr::flatten($array, 2));
}

public function testFlatMap()
{
$array = [
['name' => 'taylor', 'hobbies' => ['programming', 'basketball']],
['name' => 'adam', 'hobbies' => ['music', 'powerlifting']],
];
$hobbies = Arr::flatMap($array, function ($person) {
return $person['hobbies'];
});
$this->assertEquals(['programming', 'basketball', 'music', 'powerlifting'], $hobbies);
}

public function testGet()
{
$array = ['products.desk' => ['price' => 100]];
Expand Down