这里涉及到两个函数。
- 搜索数组中是否存在给定字符串
- 如果未找到该字符串,则使用unset()函数取消设置数组项
$array = array("first", "second", "third", "fourth");
foreach($array as $i=> $string) {
if(stristr($string, "e")) {
unset($array[$i]);
}
}
second
是包含字符 'e' 的数组项。如果它被 unset
,$array[1]
将变为空:
$array[0] = "first"
$array[1] = ""
$array[2] = "third"
$array[3] = "fourth"
我希望将数组中的$array[1]
移除(就像array_shift()
一样),这样third
就会取代second
的位置,fourth
就会取代third
的位置:
$array[0] = "first"
$array[1] = "third"
$array[2] = "fourth"