## where
where函數會遍歷當前數據源,并為每一項元素都應用傳入的過濾函數,最后返回滿足條件的元素的集合。
例子:
~~~
//創建數據源
$dataSource = Traversable::from(['foo', 'bar', 'baz', 'tear', 'cow', 'tripod', 'whisky', 'sand', 'which']);
//根據where函數傳入的Lambda表達式,來篩選數據源
$result=$dataSource->where(function($e){
return strlen($e)>=4; //字符長度大于等于4的元素
});
foreach ($result as $s){
dump($s);
}
~~~
打印的結果:
~~~
"tear"
"tripod"
"whisky"
"sand"
"which"
~~~
例子:
~~~
$dataSource = Traversable::from([1,4,8,5,10,43,53,12,0]);
$result=$dataSource->where(function($e){
return $e>=10;
});
foreach ($result as $s){
dump($s);
}
~~~
打印結果:
~~~
10
43
53
12
~~~