根据字符长度删除数组元素

Remove array element based on its character length

我在 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"  

使用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 ) 

您只需将 array_filterstrlen

一起使用即可
$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_filter

array_walk