(I'm a beginner)
我的脚本使用标准的
$c = 0;
$t = count($array);
while ($c < $t) {
$blah = $array[$c];
++$c;
}
我曾经在IT技术领域涉猎颇深。但最近遇到一个问题,需要使用array_diff
函数,但由于数组中的数值键存在间隙,导致程序出现了严重错误,提示Undefined offset
。
请问如何重置数组中的数值键?无需考虑对象在数组中的顺序。
您不需要重置数组的键:您需要改变遍历数组的方式。
不要使用while
循环并通过索引访问数组元素,而应该使用foreach循环,它只会获取数组中的元素:
foreach ($array as $key => $value) {
// $key contains the index of the current element
// $value contains the value of the current element
}
谢谢 Tatu。
为了好玩,我将与您分享我在等待明智答案时使用的愚蠢黑客:
$badArray = array_diff($allData, $myData);
$string = implode(",",$badArray);
$dump = explode(",",$string);
$goodArray = $dump;
它起作用了。让我感觉很肮脏,但它起作用了。
array_values
,所以我没有在我的答案中提及。 - Pascal MARTIN