取消设置元素后重新设置数组键的基数 [英] Rebase array keys after unsetting elements

查看:96
本文介绍了取消设置元素后重新设置数组键的基数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组:

$array = array(1,2,3,4,5);

如果我要转储数组的内容,它们将如下所示:

If I were to dump the contents of the array they would look like this:

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

当我循环浏览并取消设置某些键时,索引将全部抬起.

When I loop through and unset certain keys, the index gets all jacked up.

foreach($array as $i => $info)
{
  if($info == 1 || $info == 2)
  {
    unset($array[$i]);
  }
}

随后,如果我现在又进行了一次转储,它将看起来像:

Subsequently, if I did another dump now it would look like:

array(3) {
  [2] => int(3)
  [3] => int(4)
  [4] => int(5)
}

是否有适当的方法来重置数组,以便其元素再次基于零?

Is there a proper way to reset the array so it's elements are Zero based again ??

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

推荐答案

尝试一下:

$array = array_values($array);

使用 array_values()

这篇关于取消设置元素后重新设置数组键的基数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆