search
Homephp教程php手册如何删除PHP数组中的元素(unset,array_splice)?

如何删除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获取数组的最后一个元素
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor