7

给定以下数组:

Array
(
    [0] => Array
        (
            [id_contract] => 1
            [contract_months] => 5
            [months_with_expenses] => 1
        )

    [1] => Array
        (
            [id_contract] => 2
            [contract_months] => 12
            [months_with_expenses] => 0
        )

    [2] => Array
        (
            [id_contract] => 3
            [contract_months] => 1
            [months_with_expenses] => 1
        )

)

如何从数组中删除键“contract_months”与键“month_with_expenses”不匹配的所有元素?

我正在使用 PHP。

4

3 回答 3

8

尝试这个:

foreach ($array as $key => $element) {
    if (conditions) {
        unset($array[$key]);
    }
}
于 2013-01-05T11:35:07.883 回答
4

你可以试试这个:

foreach($arr as $key=>$value) {
    if($value['contract_months'] != $value['months_with_expenses']) {
       unset($arr[$key]);
    }
}
于 2013-01-05T11:36:04.027 回答
0

带有 if 条件和未设置的简单循环将起作用

<?php
    for( i=0; i<count($arr); i++){
        if($arr[i]['contract_months'] != $arr[i]['months_with_expenses']) {
            unset($arr[i]);
        }
    }
于 2013-01-05T11:34:48.007 回答