php删除数组中的空元素_PHP | 从数组中删除所有出现的元素
php刪除數組中的空元素
Given an array and we have to remove all occurrences of an element from it.
給定一個數組,我們必須從中刪除所有出現的元素。
array_diff()函數 (array_diff() function)
To remove all occurrences of an element or multiple elements from an array – we can use array_diff() function, we can simply create an array with one or more elements to be deleted from the array and pass the array with deleted element(s) to the array_diff() as a second parameter, first parameter will be the source array, array_diff() function returns the elements of source array which do not exist in the second array (array with the elements to be deleted).
要從數組中刪除所有出現的一個元素或多個元素 –我們可以使用array_diff()函數 ,我們可以簡單地創建一個包含一個或多個要從該數組中刪除的元素的數組,并將帶有已刪除元素的數組傳遞給array_diff()作為第二個參數,第一個參數將是源數組, array_diff()函數返回第二個數組中不存在的源數組元素(帶有要刪除的元素的數組)。
PHP code to remove all occurrences of an element from an array
PHP代碼從數組中刪除所有出現的元素
<?php //array with the string elements $array = array('the','quick','brown','fox','quick','lazy','dog');//array with the elements to be delete $array_del = array('quick');//creating a new array without 'quick' element $array1 = array_values(array_diff($array,$array_del)); //printing the $array1 variable var_dump($array1);//now we are removing 'the' and 'dog' //array with the elements to be delete $array_del = array('the', 'dog');//creating a new array without 'the' and 'dog' elements $array2 = array_values(array_diff($array,$array_del)); //printing the $array2 variable var_dump($array2); ?>Output
輸出量
array(5) {[0]=> string(3) "the"[1]=> string(5) "brown"[2]=> string(3) "fox"[3]=> string(4) "lazy" [4]=> string(3) "dog" } array(5) {[0]=> string(5) "quick"[1]=> string(5) "brown"[2]=> string(3) "fox"[3]=> string(5) "quick"[4]=> string(4) "lazy" }Explanation:
說明:
We use the array_diff() method to calculate the difference between two arrays which essentially eliminates all the occurrences of an element from $array, if they appear in $array_del. In the given example, we delete all the occurrences of the words quick and brown from $array using this method.
我們使用array_diff()方法來計算兩個數組之間的差,如果它們出現在$ array_del中 ,則基本上消除了$ array中所有元素的出現。 在給定的示例中,我們使用此方法從$ array中刪除了出現的所有quick和brown單詞。
翻譯自: https://www.includehelp.com/php/delete-all-occurrences-of-an-element-from-an-array.aspx
php刪除數組中的空元素
總結
以上是生活随笔為你收集整理的php删除数组中的空元素_PHP | 从数组中删除所有出现的元素的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java BigInteger类| bi
- 下一篇: java 根据类名示例化类_Java L