Using array_search()
and unset
, try the following:
(使用array_search()
和unset
,尝试以下操作:)
if (($key = array_search($del_val, $messages)) !== false) {
unset($messages[$key]);
}
array_search()
returns the key of the element it finds, which can be used to remove that element from the original array using unset()
.
(array_search()
返回找到的元素的键,可以使用unset()
从原始数组中删除该元素。)
It will return FALSE
on failure, however it can return a false-y value on success (your key may be 0
for example), which is why the strict comparison !==
operator is used. (如果失败,它将返回FALSE
,但是如果成功,它将返回false -y值(例如,您的键可能为0
),这就是为什么要使用严格比较!==
运算符的原因。)
The if()
statement will check whether array_search()
returned a value, and will only perform an action if it did.
(if()
语句将检查array_search()
是否返回值,并且只会执行一个动作。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…