如何找到数组中元素的长度,以确保每个元素的长度都超过2个字符。使用php也就是...
例如。$this_array=array ("this",“$this_array=array”,"this","and","th")
发布于 2012-07-27 13:01:21
循环可能有点夸张,除非你需要做更多的事情--过滤要有效得多。
<?
$this_array = array("a", "this", "and", "this", "and", "th");
$this_array = array_filter($this_array, function($val){return strlen($val)>=2;});
print_r($this_array); // Array ( [1] => this [2] => and [3] => this [4] => and [5] => th )注意:这只适用于PHP 5.3+。
发布于 2012-07-27 12:59:25
foreach($this_array as $val) {
$valLength = strlen($val); //gives length
if($valLength < 3) {
//something here, less than 2 char
}
}发布于 2012-07-27 13:00:17
foreach ($this_array as $key => $value)
{
if (strlen($value) < 3)
{
echo "{$value} is too short<br />";
}
}请参阅foreach
https://stackoverflow.com/questions/11681671
复制相似问题