I’m sure this is an easy solution – I wrote found this endswith function and thought I’d try the array_walk function instead of testing each string separately. I’d assumed that the result of the array_walk function would be false but it returns 1…How do I get it to test all the strings and return false if it didn’t find a match? Thanks
class {
function endsWith($value,$key,$haystack)
{
$length = strlen($value);
if ($length == 0) {
return true;
}
return (substr($haystack, -$length) === $value);
}
function thing()
{
$email = "should@returnfalse.info";
$arr = array("@test.net","@test.org.uk","@test.co.uk","@test.com");
echo array_walk($arr,array($this,"endsWith"),$email);
}
}
The return value of
array_walkis not determined by whatever the callback does; it only informs you if walking the entire array was completed successfully.You may want to look into a few alternatives.
This will return the number of matching elements and will also serve as a boolean test, but it will evaluate every element no matter what:
This will stop evaluating elements with
endsWithas soon as a match is detected and will returntrueif there is a match,falseotherwise: