forked from maciejczyzewski/bottomline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
where.php
47 lines (42 loc) · 875 Bytes
/
where.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
44
45
46
47
<?php
namespace collections;
/**
* Return data matching specific key value condition.
*
* **Usage**
*
* ```php
* $a = [
* ['name' => 'fred', 'age' => 32],
* ['name' => 'maciej', 'age' => 16]
* ];
*
* __::where($a, ['age' => 16]);
* ```
*
* **Result**
*
* ```
* [['name' => 'maciej', 'age' => 16]]
* ```
*
* @todo: implement compatibility with more than 2 dimensial arrays:
*
* @param array $array array of values
* @param array $cond condition in format of ['KEY'=>'VALUE']
*
* @return array
*/
function where(array $array = [], array $cond = [])
{
$result = [];
foreach ($array as $arrItem) {
foreach ($cond as $condK => $condV) {
if (!isset($arrItem[$condK]) || $arrItem[$condK] !== $condV) {
continue 2;
}
}
$result[] = $arrItem;
}
return $result;
}