Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove an element from an array when a condition is met [closed]

given the following array:

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
        )

)

How can I remove all elements from the array where the key "contract_months" doesn't match the key "month_with_expenses"?

I'm using PHP.

like image 755
Psyche Avatar asked Jan 05 '13 11:01

Psyche


2 Answers

Try this:

foreach ($array as $key => $element) {
    if (conditions) {
        unset($array[$key]);
    }
}
like image 74
suresh.g Avatar answered Oct 02 '22 23:10

suresh.g


You can try this :

foreach($arr as $key=>$value) {
    if($value['contract_months'] != $value['months_with_expenses']) {
       unset($arr[$key]);
    }
}
like image 45
Angel Avatar answered Oct 02 '22 22:10

Angel