我在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"
答案 0 :(得分:5)
您只需使用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);
答案 1 :(得分:2)
使用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 )