0

我有以下代码从数组中删除一个元素:

for ($i = 0; $i < count($contracte); $i++) {  
    if ($contracte[$i]['total_luni_contract'] == $contracte[$i]['luni_contract_cu_cheltuieli']) {
        unset($contracte[$i]);        
    }
}

数组如下所示:

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
        )

)

运行代码后,数组保持不变。它应该取消设置$contracte[1],$contracte[2]和。$contracte[7]$contracte[20]

有什么想法吗?

4

3 回答 3

1

而是在阵列上运行foreach。已经证明,从当前使用 foreach 循环迭代的数组中删除键是完全安全的。

foreach($contracte as $cheie => $contract) {
   if ($contract['total_luni_contract'] == $contract['luni_contract_cu_cheltuieli']) {
      unset($contracte[$cheie]);
   }
}
于 2013-02-18T09:29:00.057 回答
1

问题:当循环到达第二个元素 (1) 时,它会取消设置,因此 2 处的元素会到达该位置。所以在下一个循环中它会检查 $i=1 这将是下一个元素。所以(2)被跳过了。

解决方案 :

使用 foreach

foreach($contracte as $key=> $val) {
   if ($val['total_luni_contract'] == $val['luni_contract_cu_cheltuieli']) {
      unset($contracte[$key]);
   }
}

或者

将键设为字符串 - 将其设为关联数组,然后设为您的解决方案。

如果您的数组是这样的:它将起作用

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] 
于 2013-02-18T09:33:34.320 回答
0

尝试改用 array_splice

array_splice($contracte, $i, 1);

此外,当您从数组中删除某些内容时,您需要减少 $i ,以便它会第二次检查位置 $ia 处的项目。

于 2013-02-18T09:28:48.480 回答