forked from maciejczyzewski/bottomline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpick.php
43 lines (41 loc) · 1019 Bytes
/
pick.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
namespace collections;
/**
* Returns an array having only keys present in the given path list.
*
* Values for missing keys values will be filled with provided default value.
*
* **Usage**
*
* ```php
* __::pick(
* [
* 'a' => 1,
* 'b' => ['c' => 3, 'd' => 4]
* ],
* ['a', 'b.d']
* );
* ```
*
* **Result**
*
* ```
* [
* 'a' => 1,
* 'b' => ['d' => 4]
* ]
* ```
*
* @param array|object $collection The collection to iterate over.
* @param array $paths Array paths to pick
* @param mixed $default The default value that will be used if the
* specified path does not exist.
*
* @return array
*/
function pick($collection = [], array $paths = [], $default = null)
{
return \__::reduce($paths, function ($results, $path) use ($collection, $default) {
return \__::set($results, $path, \__::get($collection, $path, $default));
}, \__::isObject($collection) ? new \stdClass() : []);
}