Skip to content

Instantly share code, notes, and snippets.

@dholmes
Created September 7, 2011 19:46
Show Gist options
  • Save dholmes/1201528 to your computer and use it in GitHub Desktop.
Save dholmes/1201528 to your computer and use it in GitHub Desktop.
Gone years without doing this....the flexible array of array / array of object filter
<!-- In some controller, etc somewhere -->
<?php
// Say you find yourself with a big array of data
$messageList = $email->getCompetitionMessages($competition);
// but you want to filter out anything from the admin
$messageList = array_filter($messageList,new Istart_Filter_ValueInObjectArrayFilter('fromEmail','admin@example.com'));
// Or, if < PHP 5.3
$messageList = array_filter($messageList, array(new Istart_Filter_ValueInObjectArrayFilter('fromEmail','admin@example.com'),'valueNotIn'));
// I'm using ZF, so I just put this in my DH_Filter_ValueInObjectArrayFilter
?>
// ValueInObjectArrayFilter.php :
<?php
class DH_Filter_ValueInObjectArrayFilter
{
protected $propertyName;
protected $value;
public function __construct ($propertyName,$value)
{
$this->propertyName = $propertyName;
$this->value = $value;
}
public function valueNotIn($o)
{
if(is_object($o)){
$propertyName = $this->propertyName;
return ( strtolower($o->$propertyName) != strtolower($this->value) );
} elseif (is_array($o)){
return ( strtolower($o[$this->propertyName]) != strtolower($this->value));
}
return true;
}
public function __invoke ($o)
{
return $this->valueNotIn($o);
}
}
?>
<!-- WARNING - The above needs lots of error checking!! It's just an example / reminder -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment