php unset函數_PHP | 使用unset()函數從數組中刪除元素

php unset函數

Given an array and we have to remove an element from the array.

給定一個數組,我們必須從數組中刪除一個元素。

unset()函數 (unset() function)

To remove an element from an array, we can use a PHP library unset() function, it accepts the index and removes the element exists on the specified index.

要從數組中刪除元素 ,我們可以使用PHP庫unset()函數 ,該函數接受索引并刪除指定索引上存在的元素。

We are also using another function var_dump() – which dumps the variable details i.e. here, it will print the array variable.

我們還使用了另一個函數var_dump() -轉儲變量的詳細信息,即在這里,它將打印數組變量。

PHP code to remove an element from an array

PHP代碼從數組中刪除元素

<?php
//PHP code to remove an element from an array 
//declaring an array of strings
$array = array('the','quick','brown','fox');
//printing the array variable
var_dump($array);
//removing element from 1st index
unset ($array[1]);
//again, printing the array variable
var_dump($array);
//assigning the array after removing its element
//from 1st index to the new array
$array_new=array_values($array);
//printing the new array variable
var_dump($array_new);
?>

Output

輸出量

array(4) {
[0]=>
string(3) "the"
[1]=>
string(5) "quick"  
[2]=>
string(5) "brown"  
[3]=>
string(3) "fox" 
}
array(3) {
[0]=>
string(3) "the" 
[2]=>
string(5) "brown"  
[3]=>
string(3) "fox" 
}
array(3) {
[0]=>
string(3) "the" 
[1]=>
string(5) "brown"  
[2]=>
string(3) "fox" 
}

Explanation:

說明:

Here, We've created an array ($array) and then used the PHP unset() method to remove index 1 (which is the 2nd value since array starts from 0). Once that's removed, we print the array using var_dump but there is a problem that the indexes haven't updated. So, we create $array_new by using array_values() method on the existing $array.

在這里,我們創建了一個數組( $ array ),然后使用PHP unset()方法刪除了索引1(這是第二個值,因為array從0開始)。 刪除后,我們使用var_dump打印數組,但是存在索引尚未更新的問題。 因此,我們通過在現有$ array上使用array_values()方法創建$ array_new 。

翻譯自: https://www.includehelp.com/php/delete-an-element-from-an-array-using-unset-function.aspx

php unset函數

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/542400.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/542400.shtml
英文地址,請注明出處:http://en.pswp.cn/news/542400.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

vi顯示行號

vi顯示行號 :set nu 帶行號查看&#xff0c;并不改變文件內容:set nonu 取消帶行號查看在每個用戶的主目錄下,都有一個 vi 的配置文件".vimrc"或".exrc"用戶可以編輯它,使這些設置在每次啟動 vi 時,都有效.例如,加入如下設置行:set nu 顯示行號…

對象過濾某個屬性 循環 php_37道PHP面試題(附答案)

1、什么事面向對象&#xff1f;主要特征是什么&#xff1f;面向對象是程序的一種設計方式&#xff0c;它利于提高程序的重用性&#xff0c;使程序結構更加清晰。主要特征&#xff1a;封裝、繼承、多態。2、SESSION 與 COOKIE的區別是什么&#xff0c;請從協議&#xff0c;產生的…

項響琴C語言書籍在線瀏覽,電子琴 c語言程序

實用#include unsigned char code table[]{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};unsigned char temp;unsigned char key;unsigned char i,j;unsigned char STH0;unsigned char STL0;unsigned int code tab[]{64021,64103,64260,…

Java File類boolean createNewFile()方法(帶示例)

文件類布爾型createNewFile() (File Class boolean createNewFile()) This method is available in package java.io.File.createNewFile(). 軟件包java.io.File.createNewFile()中提供了此方法。 This method is used to create a new file by using createNewFile() method a…

oracle ? SQL執行過程

1.sql執行過程1>解析&#xff08;判斷對象是否存在&#xff0c;是否有權限查詢&#xff0c;語義解析&#xff0c;檢查緩存中是否有相同的SQL等等&#xff09;2>優化&#xff08;CBO確定優化模式&#xff0c;確定訪問路徑&#xff0c;聯接順序&#xff0c;過程中通過很多綜…

vue-video-player修改src就會報錯_4、修改入口點代碼

在riscv上電時&#xff0c;會進行CPU自檢&#xff0c;然后跳轉到bootloader處執行。bootloader設置好kernel的運行環境后&#xff0c;從硬盤加載kernel到內存&#xff0c;最后再跳轉到kernel入口地址。我們采用的bootloader為OpenSBI&#xff0c;被加載到0x80000000地址&#x…

數碼管超聲波c語言黑51,51單片機開發板-超聲波測距-數碼管顯示

《51單片機開發板-超聲波測距-數碼管顯示》由會員分享&#xff0c;可在線閱讀&#xff0c;更多相關《51單片機開發板-超聲波測距-數碼管顯示(16頁珍藏版)》請在人人文庫網上搜索。1、計算機技術系項目工作報告課程名稱單片機開發板設計與制作實訓班級學號姓名項目名稱超聲波測距…

java 方法 示例_Java ArrayDeque帶有示例的removeFirstOccurrence()方法

java 方法 示例ArrayDeque類removeFirstOccurrence()方法 (ArrayDeque Class removeFirstOccurrence() method) removeFirstOccurrence() method is available in java.lang package. removeFirstOccurrence()方法在java.lang包中可用。 removeFirstOccurrence() method is use…

社交應用動態九宮格圖片的規則

這里主要以微信和QQ空間為作為研究對象&#xff0c;得到的結論如下。 QQ空間里的動態 iOS設備&#xff0c;以iPhone6為分界 iPhone6及以上分辨率的設備&#xff1a; 當寬且高同時 > 512px時&#xff0c;判斷 寬/高的比例值&#xff1a;大于 2時&#xff0c;以高度為基準&…

c語言實現鏈表結構6,用c語言實現的鏈表結構--數據結構實驗

該樓層疑似違規已被系統折疊 隱藏此樓查看此樓#include"stdio.h"//使用new指針來將臨時變量重新初始化#include"stdio.h"typedef int ElemType;typedef struct LNode{ElemType data;struct LNode *next;}LNode,*LinkList;void InitList(LinkList &L)//…

splunk中 如何隱藏input_翻糖制作中,如何避免裂縫,如何隱藏裂縫,如何防粘?...

翻糖蛋糕 因精致的樣子和栩栩如生的各種造型深得人們的喜愛&#xff0c;它不僅滿足了人們對蛋糕口味及裝飾日益多樣化的需求&#xff0c;同時也在動手制作的過程中&#xff0c;享受到美食與生活的無窮樂趣。不過裂縫&#xff0c;不平整&#xff0c;干燥對翻糖作品來說無疑是噩夢…

Java DataInputStream readUnsignedByte()方法(帶示例)

DataInputStream類readUnsignedByte()方法 (DataInputStream Class readUnsignedByte() method) readUnsignedByte() method is available in java.io package. readUnsignedByte()方法在java.io包中可用。 readUnsignedByte() method is used to read 1 byte (i.e. 8 bit) of …

wpf中groupbox有什么用_展示設計中的標攤是什么 用的什么材料

經常聽從事展示設計的工作人員說起標攤&#xff0c;那什么是標攤呢&#xff1f;顧名思義&#xff0c;標攤就是通用標準的國際展會攤位的縮寫。但是不少人看到干巴巴的詞語還是不能理解。那么這篇文章從用途、材料等方面來詳細介紹標攤究竟是什么。 標攤的主要材質是什么一般來說…

Java BigInteger類| nextProbablePrime()方法與示例

BigInteger類nextProbablePrime()方法 (BigInteger Class nextProbablePrime() method) nextProbablePrime() method is available in java.math package. nextProbablePrime()方法在java.math包中可用。 nextProbablePrime() method is used to get the next probable prime n…

SQL 行轉列的兩種做法

if object_id(tb)is not null drop table tbGocreate table tb(姓名 varchar(10),課程 varchar(10),分數 int)insert into tb values(張三,語文,74)insert into tb values(張三,數學,83)insert into tb values(張三,物理,93)insert into tb values(李四,語文,74)insert into tb…

android一個工程的xml怎么引用另外一個工程的xml,如何在Android中使用XML引用庫中另一個包的字符串?...

Androiddocumentation告訴我,我可以使用“包名”訪問另一個包中的字符串,無論這意味著什么&#xff1a;[:]/所以在我的清單中,我想訪問一個字符串,我已將其放在一個單獨的庫項目中,在com.globalmentor.android包中 – 畢竟我的R類就是這樣&#xff1a;android:label"com.g…

kotlin 類和對象_Kotlin程序| 類和對象的示例(帶有學生數據)

kotlin 類和對象In the below program, we are creating a student class to input and print the student data like name, age. It is a simple example of creating class in Kotlin. 在下面的程序中&#xff0c;我們將創建一個學生班級&#xff0c;以輸入和打印學生數據&am…

python 復數數組_python的數組運算及推導式的運用

運行方式&#xff1a;縮進 TAb(四格)或者space(1格)注釋&#xff1a;#可以在程序后注釋文字 或者 ... &#xff0c;""" ... """ 可以多行注釋 中間全為注釋數值輸出 print(...)圖形輸出 import matplotlib as pltplt.show()列表 list[] #字符串…

ActiveX: 如何用.inf和.ocx文件生成cab文件

ActiveX: 如何用.inf和.ocx文件生成cab文件 轉載于:https://www.cnblogs.com/time-is-life/p/5977962.html

Android工具里沒有Android,android – AppCompat工具欄沒有顯示

在主題中聲明.NoActionBar之后,以及將工具欄放在布局中,我的工具欄不會顯示.我最終得到的正是你在宣布沒有動作欄時所期望的 – 沒有動作欄.這是布局&#xff1a;activity_home.xml&#xff1a;layout"layout/app_bar_home"android:layout_width"match_parent&q…