I want to check if any values inside my PHP array is longer than 5 words, using one-liner / simple method.
Output should be a boolean indicating the result.
This is what I come up with:
$isAnyValueTooLong = count(array_filter($array, function ($var)
{
return strlen($var) > 5;
})) > 0;
So it can be used like so:
$array = ["1234", "123456"]; // This should be fail
//$array = ["1234", "1", "123" ]; // This should be OK
$isAnyValueTooLong = count(array_filter($array, function ($var)
{
return strlen($var) > 5;
})) > 0;
if($isAnyValueTooLong){
echo "Error, one of the item is longer than 5";
} else {
echo "Ok, no error";
}
However, my method is hard to read and complex. My question is, any one-liner to achieve the above requirement?
Performance is not of concern as it is just a simple report program. Thanks.