forked from maciejczyzewski/bottomline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
where.php
33 lines (28 loc) · 857 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
<?php
namespace collections;
/**
* return data matching specific key value condition
*
**_::where($a, ['age' => 16]);
** // >> [['name' => 'maciej', 'age' => 16]]
* @todo: implement compatibility with more than 2 dimensial arrays:
**__::where($a, ['name' => 'fred & maciej', 'ages' => ['first' => 32]);
** // >> ['name' => 'fred & maciej', 'ages' => ['fred' => 32, 'maciej' => 16]]
*
* @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;
}