close

如何在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

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 dizzy03 的頭像
    dizzy03

    碎碎念

    dizzy03 發表在 痞客邦 留言(0) 人氣()