我在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
仅与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_filter
array_walk
先用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 )