Dre*_*rew 6 php arrays key numeric
(I'm a beginner)
Run Code Online (Sandbox Code Playgroud)
我的脚本使用标准
$c = 0;
$t = count($array);
while ($c < $t) {
$blah = $array[$c];
++$c;
}
Run Code Online (Sandbox Code Playgroud)
相当广泛.但是我遇到了一个我需要的情况,array_diff它打破了所有地狱,因为现在数字键有间隙.我Undefined offset到处都是错误.
如何重置阵列的数字键?数组中对象的顺序无关紧要.
Tat*_*nen 27
要重置密钥,您可以使用array_values():
$array = array_values($array);
Run Code Online (Sandbox Code Playgroud)
您不需要重置阵列的键:您必须改变您的方式.
不要使用while循环并通过索引访问数组元素,而应使用foreach循环,它只能从数组中获取元素:
foreach ($array as $key => $value) {
// $key contains the index of the current element
// $value contains the value of the current element
}
Run Code Online (Sandbox Code Playgroud)