日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php删除数组中的空元素_PHP | 从数组中删除所有出现的元素

發布時間:2025/3/11 php 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 | 从数组中删除所有出现的元素的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。