php – 刪除foreach循環中的數組元素 --unset


內容簡介:翻譯自:https://stackoverflow.com/questions/8942885/deleting-an-array-element-within-foreach-loop

本文轉載自:https://codeday.me/bug/20190112/512660.html,本站轉載出於傳遞更多信息之目的,版權歸原作者或者來源機構所有。

我有一個簡單的數組,其中包含所有國家/地區的名稱以及每個國家/地區在我的網站上注冊的用戶總數.它是這樣的:

Array ( [1] => Array ( [name] => Afghanistan [total] => 3 ) [2] => Array ( [name] => Albania [total] => 0 ) )

而且,我正在嘗試刪除擁有0個用戶的數組元素(國家/地區).

我已嘗試使用此代碼,但它無法正常工作:

foreach($country as $row) {
    if ($row['total'] == 0) {
        unset($row);
    }
}

這段代碼有什么問題?

如果取消設置($row),則只刪除局部變量.

而是獲取密鑰並刪除它:

foreach ($country as $i => $row) { if ($row['total'] == 0) { unset($country[$i]); } }

翻譯自:https://stackoverflow.com/questions/8942885/deleting-an-array-element-within-foreach-loop

轉載:https://www.codercto.com/a/51572.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM