我们删除一个array,

unset($arr);

想删除某个元素

unsert($arr[i])

一个陷阱是:

unset() 函数允许删除数组中的某个键。但要注意数组将不会重建索引。如果需要删除后重建索引,可以用 array_values() 函数。

 $a=array(1,2,3);
for($i=0;$i<sizeof($a);$i++)
{
echo $a[$i];
}
unset($a[0]); for($i=0;$i<sizeof($a);$i++)
{
echo $a[$i];
}

前面输出:1,2,3

后面输出:

Notice: Undefined offset: 0 in F:\xampp\htdocs\php\passValueByReference.php on line 84
2

为什么?

因为unset($a[0])将第1个元素给删除了,但是输出时,我们还从$i=0  开始的,当然就不对了,php可不会自动调整下标的。

 $a=array(1,2,3);
for($i=0;$i<sizeof($a);$i++)
{
echo $a[$i];
}
unset($a[0]); while(list($k,$v)=each($a))
{
echo $k.'->'.$v."<br/>";
}

还可以用array_values输出值.

<?php
$a = array(1 => 'one', 2 => 'two', 3 => 'three');
unset($a[2]);
/* will produce an array that would have been defined as
$a = array(1 => 'one', 3 => 'three');
and NOT
$a = array(1 => 'one', 2 =>'three');
*/ $b = array_values($a);
// Now $b is array(0 => 'one', 1 =>'three')
?>

array_values() 返回 input 数组中所有的值并给其建立数字索引。

<?php
$array = array("size" => "XL", "color" => "gold");
print_r(array_values($array));
?>

以上例程会输出:

Array
(
[0] => XL
[1] => gold
)

php unset 数组陷阱的更多相关文章

  1. PHP学习笔记九【数组二】

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/h ...

  2. tp使用ajaxReturn返回二维数组格式的字符串,前台如何获取非乱码

    参考: https://www.cnblogs.com/jiqing9006/p/5000849.html https://blog.csdn.net/zengxiangxuan123456/arti ...

  3. PHP常用函数、数组方法

    常用函数:rand(); 生成随机数rand(0,50); 范围随机数时间:time(); 取当前时间戳date("Y-m-d H:i:s"); Y:年 m:月份 d:天 H:当前 ...

  4. Linux Shell数组常用操作详解

    Linux Shell数组常用操作详解 1数组定义: declare -a 数组名 数组名=(元素1 元素2 元素3 ) declare -a array array=( ) 数组用小括号括起,数组元 ...

  5. shell数组操作

    1.数组定义,shell使用一对括号表示数组,数组元素间用"空格"分隔 # 空数组arr1 arr1=() # 数组arr2,成员分别是1, 2, 3, 4, 5, 6 arr2= ...

  6. shell中一维数组值得获取

    (1)数组的定义 root@tcx4440-03:~# a=(1 2 3 4) root@tcx4440-03:~# echo ${a[1]}2 root@tcx4440-03:~# a[0]=1ro ...

  7. shell之数组

    1.从数组的下标分为索引数组.关联数组 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 /* 索引数组,即通常情况下所说的数组 */ var ary1 = [1,3,5, ...

  8. 转:linux shell 数组建立及使用技巧

    linux shell在编程方面比windows 批处理强大太多,无论是在循环.运算.已经数据类型方面都是不能比较的. 下面是个人在使用时候,对它在数组方面一些操作进行的总结. 1.数组定义 [che ...

  9. Linux - 简明Shell编程07 - 数组(Array)

    脚本地址 https://github.com/anliven/L-Shell/tree/master/Shell-Basics 示例脚本及注释 #!/bin/bash test0=() # 定义数组 ...

随机推荐

  1. c#读写共享内存操作函数封装

    原文 c#读写共享内存操作函数封装 c#共享内存操作相对c++共享内存操作来说原理是一样,但是c#会显得有点复杂. 现把昨天封装的读写共享内存封装的函数记录下来,一方面希望给需要这块的有点帮助,另一方 ...

  2. zip文件压缩(转)

    zip文件结构            上面中的每一行都是一个条目,zip文件就是由一个或者多个条目组成.      条目在Java中对应ZipEntry类         创建zip压缩文件     ...

  3. poj 1836 Alignment(线性dp)

    题目链接:http://poj.org/problem?id=1836 思路分析:假设数组为A[0, 1, …, n],求在数组中最少去掉几个数字,构成的新数组B[0, 1, …, m]满足条件B[0 ...

  4. java代码发送JSON格式的httpPOST请求

    package com.test; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOE ...

  5. uva Stacks of Flapjacks

                                                     Stacks of Flapjacks  题目链接:Click Here~ 题目描写叙述:     ...

  6. Qt编写文件一键命名软件

    之所以会写这篇博文,主要是由于近期从网上下载了一堆图片,但图片名称非常没有规律,处理起来非常不方便,由此想到是不是有一键命名的软件能够帮助我对全部图片命名,是图片名称有规律,这样在处理时方便操作. 有 ...

  7. HDU-4866-Shooting(函数式线段树)

    Problem Description In the shooting game, the player can choose to stand in the position of [1, X] t ...

  8. IIS 7关闭应用程序池自动回收设置

    在web应用程序中经常有一些任务就需要在Global文件启用一个线程来实现, 那假设我们在自己的ASP.NET应用程序中加入了Quartz.NET框架,并且配置等等都OK了. 这个站点访问量很少,现在 ...

  9. SQL Server(SSIS package) call .net DLL

    There are two method to call .net DLL in SQLSERVER. The first one is to use the sql clr but it has a ...

  10. Delete website with command.

    1.AppCmd.exe 2.http://www.windowsnetworking.com/articles-tutorials/windows-server-2008/Configuring-I ...