After using array_unique
, an array without the duplicate values is removed. However, it appears that the keys are also removed, which leaves gaps in an array with numerical indexes (although is fine for an associative array). If I iterate using a for loop, I have to account for the missing indexes and just copy the keys to a new array, but that seems clumsy.
使用array_unique后,将删除没有重复值的数组。但是,似乎也删除了键,这会在数组中留下带数字索引的间隙(尽管对于关联数组来说很好)。如果我使用for循环进行迭代,我必须考虑丢失的索引并将密钥复制到新数组,但这看起来很笨拙。
3 个解决方案
#1
40
$foo = array_values($foo);
will re-number an array for you
$ foo = array_values($ foo);将为您重新编号数组
#2
1
Instead of using for loops it sounds like you should use foreach loops. Apparently you don't care about indexes anyway since you are renumbering them.
而不是使用for循环,听起来你应该使用foreach循环。显然你不关心索引,因为你正在重新编号。
This loop:
这个循环:
for ($i = 0; $i < $loopSize; $i++)
{
process($myArray[$i]);
}
turns into
变成
foreach($myArray as $key=> $value)
{
process($value);
/** or process($myArray[$key]); */
}
or even more simply
foreach($myArray as $value)
{
process($value);
}
#3
0
In the few cases I've tried using for instead of foreach, I soon regretted it.
在我尝试使用而不是foreach的少数情况下,我很快就后悔了。
It can really always be avoided, you can even use foreach but ignore the values and use the key, almost forgetting that its a foreach instead of for, but avoiding any gaps in your keys and automatically have your bounds taken care of without length/min/max functions or anything.
它总是可以避免,甚至可以使用foreach但忽略值并使用键,几乎忘记它是一个foreach而不是for,但避免键中的任何间隙并自动处理你的边界没有长度/分钟/ max函数或任何东西。
ex.
恩。
foreach($myArray as $key=>$val)
{
myArray[$key] = myFunction(myArray[$key]);
}
I've particularly found this useful with parallel arrays.
我特别发现这对并行数组很有用。
$a = getA(); $b = getB();
foreach($a as $key=>val)
{
$sql = "INSERT INTO table (field1, field2) VALUES ($a[$key], $b[$key])";
}
#1
40
$foo = array_values($foo);
will re-number an array for you
$ foo = array_values($ foo);将为您重新编号数组
#2
1
Instead of using for loops it sounds like you should use foreach loops. Apparently you don't care about indexes anyway since you are renumbering them.
而不是使用for循环,听起来你应该使用foreach循环。显然你不关心索引,因为你正在重新编号。
This loop:
这个循环:
for ($i = 0; $i < $loopSize; $i++)
{
process($myArray[$i]);
}
turns into
变成
foreach($myArray as $key=> $value)
{
process($value);
/** or process($myArray[$key]); */
}
or even more simply
foreach($myArray as $value)
{
process($value);
}
#3
0
In the few cases I've tried using for instead of foreach, I soon regretted it.
在我尝试使用而不是foreach的少数情况下,我很快就后悔了。
It can really always be avoided, you can even use foreach but ignore the values and use the key, almost forgetting that its a foreach instead of for, but avoiding any gaps in your keys and automatically have your bounds taken care of without length/min/max functions or anything.
它总是可以避免,甚至可以使用foreach但忽略值并使用键,几乎忘记它是一个foreach而不是for,但避免键中的任何间隙并自动处理你的边界没有长度/分钟/ max函数或任何东西。
ex.
恩。
foreach($myArray as $key=>$val)
{
myArray[$key] = myFunction(myArray[$key]);
}
I've particularly found this useful with parallel arrays.
我特别发现这对并行数组很有用。
$a = getA(); $b = getB();
foreach($a as $key=>val)
{
$sql = "INSERT INTO table (field1, field2) VALUES ($a[$key], $b[$key])";
}