[英]Removing an element from an array not working
I have the following code to remove an element from an array: 我有以下代码从数组中删除元素:
for ($i = 0; $i < count($contracte); $i++) {
if ($contracte[$i]['total_luni_contract'] == $contracte[$i]['luni_contract_cu_cheltuieli']) {
unset($contracte[$i]);
}
}
The array looks like this: 该数组如下所示:
Array
(
[0] => Array
(
[id_contract] => 3
[numar] => 2955
[data] => 2011-04-04
[total_luni_contract] => 2
[luni_contract_cu_cheltuieli] => 0
)
[1] => Array
(
[id_contract] => 25
[numar] => 14
[data] => 2013-01-07
[total_luni_contract] => 1
[luni_contract_cu_cheltuieli] => 1
)
[2] => Array
(
[id_contract] => 26
[numar] => 15
[data] => 2013-01-07
[total_luni_contract] => 1
[luni_contract_cu_cheltuieli] => 1
)
[3] => Array
(
[id_contract] => 27
[numar] => 16
[data] => 2013-01-07
[total_luni_contract] => 1
[luni_contract_cu_cheltuieli] => 0
)
[4] => Array
(
[id_contract] => 28
[numar] => 17
[data] => 2013-01-07
[total_luni_contract] => 1
[luni_contract_cu_cheltuieli] => 0
)
[5] => Array
(
[id_contract] => 29
[numar] => 23
[data] => 2013-01-08
[total_luni_contract] => 6
[luni_contract_cu_cheltuieli] => 0
)
[6] => Array
(
[id_contract] => 30
[numar] => 24
[data] => 2013-01-08
[total_luni_contract] => 6
[luni_contract_cu_cheltuieli] => 0
)
[7] => Array
(
[id_contract] => 31
[numar] => 33
[data] => 2013-01-09
[total_luni_contract] => 1
[luni_contract_cu_cheltuieli] => 1
)
[8] => Array
(
[id_contract] => 32
[numar] => 34
[data] => 2013-01-09
[total_luni_contract] => 1
[luni_contract_cu_cheltuieli] => 0
)
[9] => Array
(
[id_contract] => 33
[numar] => 35
[data] => 2013-01-09
[total_luni_contract] => 1
[luni_contract_cu_cheltuieli] => 0
)
[10] => Array
(
[id_contract] => 34
[numar] => 36
[data] => 2013-01-09
[total_luni_contract] => 1
[luni_contract_cu_cheltuieli] => 0
)
[11] => Array
(
[id_contract] => 35
[numar] => 37
[data] => 2013-01-09
[total_luni_contract] => 1
[luni_contract_cu_cheltuieli] => 0
)
[12] => Array
(
[id_contract] => 36
[numar] => 38
[data] => 2013-01-09
[total_luni_contract] => 1
[luni_contract_cu_cheltuieli] => 0
)
[13] => Array
(
[id_contract] => 37
[numar] => 39
[data] => 2013-01-09
[total_luni_contract] => 1
[luni_contract_cu_cheltuieli] => 0
)
[14] => Array
(
[id_contract] => 38
[numar] => 40
[data] => 2013-01-09
[total_luni_contract] => 1
[luni_contract_cu_cheltuieli] => 0
)
[15] => Array
(
[id_contract] => 39
[numar] => 41
[data] => 2013-01-09
[total_luni_contract] => 1
[luni_contract_cu_cheltuieli] => 0
)
[16] => Array
(
[id_contract] => 40
[numar] => 42
[data] => 2013-01-09
[total_luni_contract] => 1
[luni_contract_cu_cheltuieli] => 0
)
[17] => Array
(
[id_contract] => 41
[numar] => 43
[data] => 2013-01-09
[total_luni_contract] => 6
[luni_contract_cu_cheltuieli] => 0
)
[18] => Array
(
[id_contract] => 42
[numar] => 44
[data] => 2013-01-09
[total_luni_contract] => 6
[luni_contract_cu_cheltuieli] => 0
)
[19] => Array
(
[id_contract] => 43
[numar] => 45
[data] => 2013-01-09
[total_luni_contract] => 6
[luni_contract_cu_cheltuieli] => 0
)
[20] => Array
(
[id_contract] => 44
[numar] => 46
[data] => 2013-01-09
[total_luni_contract] => 6
[luni_contract_cu_cheltuieli] => 6
)
[21] => Array
(
[id_contract] => 45
[numar] => 47
[data] => 2013-01-09
[total_luni_contract] => 6
[luni_contract_cu_cheltuieli] => 6
)
[22] => Array
(
[id_contract] => 46
[numar] => 48
[data] => 2013-01-09
[total_luni_contract] => 6
[luni_contract_cu_cheltuieli] => 4
)
)
After I run the code, the array remains the same. 运行代码后,数组保持不变。 It should unset
$contracte[1]
, $contracte[2]
, $contracte[7]
and $contracte[20]
. 它应该取消设置
$contracte[1]
, $contracte[2]
, $contracte[7]
和$contracte[20]
。
Any ideeas? 任何想法?
Run a foreach on the array instead. 而是在阵列上运行foreach 。 It has been proved that it is perfectly safe to remove keys from an array that you're currently iterating on with a foreach loop.
事实证明,从当前使用foreach循环迭代的数组中删除键是完全安全的。
foreach($contracte as $cheie => $contract) {
if ($contract['total_luni_contract'] == $contract['luni_contract_cu_cheltuieli']) {
unset($contracte[$cheie]);
}
}
Issue : When loop reaches second element (1) it unsets it, so element at 2 comes to that position. 问题:当循环到达第二个元素(1)时,它会取消它,因此2处的元素到达该位置。 so in next loop it checks for $i=1 which will be next element.
所以在下一个循环中它检查$ i = 1,这将是下一个元素。 so (2) got skipped.
所以(2)被跳过了。
Solution : 方案:
Use foreach 使用foreach
foreach($contracte as $key=> $val) {
if ($val['total_luni_contract'] == $val['luni_contract_cu_cheltuieli']) {
unset($contracte[$key]);
}
}
OR 要么
Make the key as strings - make it associative array and then your solution. 将密钥设为字符串 - 使其成为关联数组,然后是解决方案。
If your array is something like this : It will work 如果你的数组是这样的:它会工作
Array
(
[one] => Array
(
[id_contract] => 3
[numar] => 2955
[data] => 2011-04-04
[total_luni_contract] => 2
[luni_contract_cu_cheltuieli] => 0
)
[two] => Array
(
[id_contract] => 25
[numar] => 14
[data] => 2013-01-07
[total_luni_contract] => 1
[luni_contract_cu_cheltuieli] => 1
)
[three] => Array
(
[id_contract] => 26
[numar] => 15
[data] => 2013-01-07
[total_luni_contract] => 1
[luni_contract_cu_cheltuieli] => 1
)
[four] => Array
(
[id_contract]
Try using array_splice instead 请尝试使用array_splice
array_splice($contracte, $i, 1); array_splice($ contracte,$ i,1);
Also, you'll want to decrement $i when you remove something from the array so it will check the item at position $ia second time. 此外,当您从阵列中删除某些内容时,您将要减少$ i,以便它将在第二次位置$ ia处检查该项目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.