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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > php >内容正文

php

php unset函数_PHP | 使用unset()函数从数组中删除元素

發(fā)布時間:2025/3/11 php 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php unset函数_PHP | 使用unset()函数从数组中删除元素 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

php unset函數(shù)

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

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

unset()函數(shù) (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.

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

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

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

PHP code to remove an element from an array

PHP代碼從數(shù)組中刪除元素

<?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.

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

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

php unset函數(shù)

總結(jié)

以上是生活随笔為你收集整理的php unset函数_PHP | 使用unset()函数从数组中删除元素的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。