Created
November 4, 2013 19:36
-
-
Save tournasdim/7308018 to your computer and use it in GitHub Desktop.
A simple implementation of SPL's FilterIterator Class .
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class CustomFilterIterator extends FilterIterator | |
{ | |
protected $filterKey ; | |
protected $filterValue ; | |
public function __construct($iter, $key , $value ) | |
{ | |
parent::__construct($iter); | |
$this->filterKey = $key; | |
$this->filterValue = $value; | |
} | |
public function accept() | |
{ | |
$item = $this->current(); | |
$key = key($this->current()) ; | |
if ( $key === $this->filterKey AND $item[$key] === $this->filterValue) | |
return true ; | |
} | |
} | |
$myArray = array( | |
['name' => 'dimitrios' , 'address' => 'SomeAddress' , 'country' => 'Greece' , 'phone'=> 4568765] , | |
['name' => 'Jhon' ,'address' => 'Alimos' , 'country' => 'France' , 'phone' => 6577474] , | |
['name' => 'Justin' , 'address' => 'Sint Johnson ' , 'country' => 'UK' , 'phone' => '8875775'] | |
); | |
$customFilter = new CustomFilterIterator(new ArrayIterator($myArray) , 'name' ,'dimitrios'); | |
foreach ($customFilter as $v) { | |
var_dump($v) ; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment