Find by Like (Continued)

78 views
Skip to first unread message

Joel

unread,
Mar 9, 2006, 10:50:44 AM3/9/06
to Php Object Generator
This is an improvement to the "Find By Like" function I proposed
earlier
(http://groups.google.com/group/Php-Object-Generator/browse_frm/thread/b9e54c25e51ffe79/)
submitted by Vitek

function filterList($objectList, $attributeToCompare, $value) {
$filteredList = array();
foreach ($objectList as $object){
if (is_array($attributeToCompare)) {
for ($i = 0; $i <
sizeof($attributeToCompare); $i++) {
if
(strpos($object->{$attributeToCompare[$i]}, $value) !== false){
$filteredList[] =
$object;
break;
}
}
} else {
if
(strpos($object->{$attributeToCompare}, $value) !== false){
$filteredList[] = $object;
}
}
}
return $filteredList;
}

----------------------
Now as $attributeToCompare it accepts array and I also added a check
!== false, because strpos can return 0, if the search is just on the
beginning - and the whole if fails even if it shouldn't

Thanks Vitek

Reply all
Reply to author
Forward
0 new messages