Skip to content

Instantly share code, notes, and snippets.

@tournasdim
Created November 4, 2013 19:36
Show Gist options
  • Save tournasdim/7308018 to your computer and use it in GitHub Desktop.
Save tournasdim/7308018 to your computer and use it in GitHub Desktop.
A simple implementation of SPL's FilterIterator Class .
<?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