我们来举一个例子:
复制代码 代码如下:
$arr = array('a','b','c','d');
unset($arr[1]);
print_r($arr);
?>
我之前想象的是unset之后,数组$arr应该会压缩数组以填补缺少的元素位置,但print_r($arr)之后,结果却不是那样的,最终结果是 Array ( [0] => a [2] => c [3] => d );
如果是这样的话,那我们就来看看数字数组的形式
复制代码 代码如下:
$arr = range(5,10,4);
print_r($arr);//Array ( [0] => 5 [1] => 6 [2] => 7 [3] => 8 [4] => 9 [5] => 10 )
unset($arr[1]);//Array ( [0] => 5 [2] => 7 [3] => 8 [4] => 9 [5] => 10 )
print_r($arr);
?>
可以看到输出的形式也是数组是会填补缺少的元素的位置。那么怎么才能做到缺少的元素会被填补并且数组会被重新索引呢?答案是array_splice():
<?php <BR><p class="codetitle"><span style="CURSOR: pointer" onclick="doCopy('code32117')"><u>复制代码</u></span> 代码如下:</p><p class="code" id="code32117"><br>$arr = array('a','b','c','d'); <br>array_splice($arr,1,1); <br>print_r($arr); // <span style="font-family: Simsun;font-size:16px; ">Array ( [0] => a [1] => c [2] => d )</span> <br>?><br></p>

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

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

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.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment