满足条件时如何从数组中删除元素 [英] How to remove an element from an array when a condition is met

查看:190
本文介绍了满足条件时如何从数组中删除元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下数组:

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"不匹配的数组中删除所有元素?

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

我正在使用PHP.

推荐答案

尝试一下:

foreach ($array as $key => $element) {
    if (conditions) {
        unset($array[$key]);
    }
}

这篇关于满足条件时如何从数组中删除元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆