如何在PHP中,依照陣列的value刪除陣列裏頭的元素?
A: 使用array_search()去搜尋key值,然後unset此陣列key值中的元素。
--
Using array_search() and unset, try the following:
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(). 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.
The if() statement will check whether array_search() returned a value, and will only perform an action if it did.
--
轉自 https://stackoverflow.com/questions/7225070/php-array-delete-by-value-not-key
文章標籤
全站熱搜
