使用数组映射,我可以获得数组中字符串的长度:
$array[] = "1";
$array[] = "22";
$array[] = "333";
$array[] = "4444";
$len = array_map("strlen", $array);
print_r($len);//Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )
现在我需要检查值是否大于1,如何做到这一点?
答案 0 :(得分:1)
如果要检查值的长度是否大于1,则可以使用:
$lengt1 = array_map(function ($v) { return strlen($v) > 1; }, $array);
foreach ($lengt1 as $key => $val) {
echo "\$lengt1[$key] = " . ($val ? 'true' : 'false') . "\n";
}
输出:
$lengt1[0] = false
$lengt1[1] = true
$lengt1[2] = true
$lengt1[3] = true
如果您需要知道该值是否为> 1,那么这将起作用:
$vgt1 = array_map(function ($v) { return (int)$v > 1; }, $array);
foreach ($vgt1 as $key => $val) {
echo "\$vgt1[$key] = " . ($val ? 'true' : 'false') . "\n";
}
输出:
$vgt1[0] = false
$vgt1[1] = true
$vgt1[2] = true
$vgt1[3] = true