array_diff &重新编号数字键
(I'm a beginner)
我的脚本相当广泛地使用了该标准
$c = 0;
$t = count($array);
while ($c < $t) {
$blah = $array[$c];
++$c;
}
。但我刚刚遇到了一种情况,我还需要 array_diff ,它打破了这一切,因为现在数字键有间隙。我到处都收到未定义的偏移量
错误。
如何重置数组的数字键?数组中对象的顺序无关。
(I'm a beginner)
My script uses the standard
$c = 0;
$t = count($array);
while ($c < $t) {
$blah = $array[$c];
++$c;
}
rather extensively. But I just ran into a situation where I also need array_diff
and it breaks that all to hell because now the numeric keys have gaps. I'm getting Undefined offset
errors all over the place.
How do I reset the numeric keys of an array? The order of the objects in the array is irrelevant.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要重置键,您可以使用
array_values()
:To reset the keys, you can use
array_values()
:您不需要重置数组的键:您必须改变您处理数组的方式。
您应该使用 foreach 循环,而不是使用 while 循环并通过索引访问数组元素,它只会从数组中获取元素:
You don't need to reset the keys of you array : you have to change the way you are going through it.
Instead of using a
while
loop and accessing the array elemnts by index, you should use a foreach loop, which will only get you elements from the array :谢谢塔图。
对于lulz,我将与您分享我在等待明智答案时使用的以下白痴黑客:
有效。让我感觉很脏,但它有效。
Thank you Tatu.
For the lulz, I will share with you the following idiot hack I used while waiting for a sensible answer:
worked. Made me feel all dirty, but it worked.