首頁 後端開發 PHP問題 php怎麼去掉陣列指定元素

php怎麼去掉陣列指定元素

Mar 15, 2021 pm 06:08 PM
php 陣列

php去掉陣列指定元素的方法:1、使用array_splice()函數,語法「array_splice(array,start,length,array)」;2、使用unset()函數,語法「unset (var) 」。

php怎麼去掉陣列指定元素

本教學操作環境:windows7系統、PHP7.1版,DELL G3電腦

php去掉陣列指定元素

方法一:

<?php
$arr1 = array(1,3, 5,7,8);
$key = array_search(3, $arr1);
if ($key !== false)
  array_splice($arr1, $key, 1);
var_dump($arr1);
?>

#輸出:

array(4) { [0]=> int(1) [1]=> int(5) [2]=> int(7) [3]=> int(8) }

方法二:

<?php
$arr2 = array(1,3, 5,7,8);
foreach ($arr2 as $key=>$value)
{
  if ($value === 3)
    unset($arr2[$key]);
}
var_dump($arr2);
?>

輸出:

array(4) { [0]=> int(1) [2]=> int(5) [3]=> int(7) [4]=> int(8) }

小結:

可以看到使用array_splice()刪除特定值和使用unset刪除特定值是有區別的。

array_splice()函數刪除的話,陣列的索引值也改變了。

unset()函數刪除的話,陣列的索引值沒有改變。

推薦學習:《PHP影片教學

以上是php怎麼去掉陣列指定元素的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

PHP教程
1587
276
Edge PDF查看器不起作用 Edge PDF查看器不起作用 Aug 07, 2025 pm 04:36 PM

testthepdfinanotherapptoderineiftheissueiswiththefileoredge.2.enablethebuilt inpdfviewerbyTurningOff“ eflblyopenpenpenpenpenpdffilesexternally”和“ downloadpdffiles” inedgesettings.3.clearbrowsingdatainclorwearbrowsingdataincludingcookiesandcachedcachedfileresteroresoreloresorelorsolesoresolesoresolvereresoreorsolvereresoreolversorelesoresolvererverenn

VS代碼快捷方式專注於Explorer面板 VS代碼快捷方式專注於Explorer面板 Aug 08, 2025 am 04:00 AM

VSCode中可通過快捷鍵快速切換面板與編輯區。要跳轉至左側資源管理器面板,使用Ctrl Shift E(Windows/Linux)或Cmd Shift E(Mac);返回編輯區可用Ctrl `或Esc或Ctrl 1~9。相比鼠標操作,鍵盤快捷鍵更高效且不打斷編碼節奏。其他技巧包括:Ctrl KCtrl E聚焦搜索框,F2重命名文件,Delete刪除文件,Enter打開文件,方向鍵展開/收起文件夾。

修復:Windows Update無法安裝 修復:Windows Update無法安裝 Aug 08, 2025 pm 04:16 PM

runthewindowsupdatetrubloubleshooterviaSettings>更新&安全> is esseShootsoAtomationfixCommonissues.2.ResetWindowSupDateComponentsByStoppingRealatedServices,RenamingTheSoftWaredWaredWaredSoftwaredSistribution andCatroot2Folders,intrestrestartingthertingthertingtherserviceSteStoceTocle

以示例運行子過程 以示例運行子過程 Aug 06, 2025 am 09:05 AM

使用os/exec包運行子進程,通過exec.Command創建命令但不立即執行;2.使用.Output()運行命令並捕獲stdout,若退出碼非零則返回exec.ExitError;3.使用.Start()非阻塞啟動進程,結合.StdoutPipe()實時流式輸出;4.通過.StdinPipe()向進程輸入數據,寫入後需關閉管道並調用.Wait()等待結束;5.必須處理exec.ExitError以獲取失敗命令的退出碼和stderr,避免殭屍進程。

掌握foreach內部使用休息,繼續和goto的流量控制 掌握foreach內部使用休息,繼續和goto的流量控制 Aug 06, 2025 pm 02:14 PM

breakexitstheloopimmediatelyafterfindingatarget,idealforstoppingatthefirstmatch.2.continueskipsthecurrentiteration,usefulforfilteringitemsliketemporaryfiles.3.gotojumpstoalabeledstatement,acceptableinrarecaseslikecleanuporerrorhandlingbutshouldbeused

修復:以太網'身份不明網絡” 修復:以太網'身份不明網絡” Aug 12, 2025 pm 01:53 PM

Restartyourrouterandcomputertoresolvetemporaryglitches.2.RuntheNetworkTroubleshooterviathesystemtraytoautomaticallyfixcommonissues.3.RenewtheIPaddressusingCommandPromptasadministratorbyrunningipconfig/release,ipconfig/renew,netshwinsockreset,andnetsh

比較和對比PHP特徵,抽像類別和界面與實際用例。 比較和對比PHP特徵,抽像類別和界面與實際用例。 Aug 11, 2025 pm 11:17 PM

Useinterfacestodefinecontractsforunrelatedclasses,ensuringtheyimplementspecificmethods;2.Useabstractclassestosharecommonlogicamongrelatedclasseswhileenforcinginheritance;3.Usetraitstoreuseutilitycodeacrossunrelatedclasseswithoutinheritance,promotingD

python檢查字符串是否是編號示例 python檢查字符串是否是編號示例 Aug 06, 2025 am 07:42 AM

isdigit()僅適用於正整數,不支持小數、負數和科學計數法;2.isnumeric()支持更多Unicode數字如分數,但仍不支持小數點和負號;3.replace配合isdigit可判斷整數和小數,但不支持科學計數法;4.try-except嘗試float轉換是最通用的方法,支持整數、小數、負數和科學計數法,推薦用於通用場景;5.正則表達式可精確控制數字格式,但編寫複雜易出錯;總結:最實用的是第4種方法,簡單且全面支持各種數字形式,以完整句⼦結束。

See all articles