如何删除PHP数组中的元素(unset,array_splice)?
如果要在某个数组中删除一个元素,可以直接用的unset,但是数组的索引不会重排:
<?php $arr = array('a','b','c','d'); unset($arr[1]); print_r($arr);
结果是:
Array ( [0] => a [2] => c [3] => d )
那么怎么才能做到缺少的元素会被填补并且数组会被重新索引呢?答案是array_splice():
<?php $arr = array('a','b','c','d'); array_splice($arr,1,1); print_r($arr);
结果是:
Array ( [0] => a [1] => c [2] => d )
您可能感兴趣的文章
- PHP去除数组中的空值元素(array_filter)
- php在数组中查找某个值是否存在(in_array(),array_search(),array_key_exists())
- php如何删除数组的第一个元素和最后一个元素
- JavaScript 数组操作函数总结(push,pop,join,shift,unshift,slice,splice,concat)
- PHP合并数组+与array_merge的区别
- php清除数组中的空值元素
- PHP数组函数array_walk()笔记
- php获取数组的最后一个元素

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver Mac version
Visual web development tools