从数组中删除元素不起作用

时间:2013-02-18 09:22:37

标签: php arrays unset

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

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]

任何想法?

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分: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]);
   }
}

OR

将密钥设为字符串 - 使其成为关联数组,然后是解决方案。

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

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] 

答案 2 :(得分:0)

尝试使用array_splice

array_splice($ contracte,$ i,1);

此外,当您从阵列中删除某些内容时,您将要减少$ i,以便它再次检查$ i位置的项目。