0

我有一个数组PHP

$array = array("Linda","Chadwick","Bari","Angela","Marco");

所以,

 $array[0]="Linda"  
 $array[1]="Chadwick"  
 $array[2]="Bari"  
 $array[3]="Angela"  
 $array[4]="Marco"  

我想删除具有字符串长度的名称<=4

这样,按键就被调整了。

$array[0]="Linda"
$array[1]="Chadwick"  
$array[2]="Angela"  
$array[3]="Marco"  
4

2 回答 2

10

您可以简单地使用它array_filter仅与strlen

$array = array("Linda","Chadwick","Bari","Angela","Marco");
$result = array_filter($array,function($v){ return strlen($v) > 4; });
print_r($result);

$result = array();
array_walk($array,function($v) use (&$result){ if(strlen($v) > 4){$result[] = $v;} });
print_r($result);

数组过滤器

array_walk

于 2015-06-08T08:47:01.020 回答
5

使用array_filter然后array_values重置键序列:

$array = array("Linda","Chadwick","Bari","Angela","Marco");
$array = array_values(array_filter($array, function($v) {
    return strlen($v) > 4;
}));
print_r($array);

输出:

Array ( [0] => Linda [1] => Chadwick [2] => Angela [3] => Marco ) 
于 2015-06-08T07:39:17.257 回答