根据条件从数组中删除子数组

时间:2019-04-24 16:27:43

标签: php arrays

我想根据其元素从数组中删除子数组,例如如果它包含键"TOTO"的值"ppd name"(在变量中)

我的数组是:

Array
(
    [1] => Array
        (
            [id] => R419FD00Customers1556106426
            [main] => Array
                (
                    [release] => R419
                    [delivery] => FD00
                    [upgrade Type] => Customers
                    [upgrade date] => 2019-04-11
                )

        )

    [2] => Array
        (
            [id] => R422Beta1internal1556107476
            [main] => Array
                (
                    [release] => R422
                    [delivery] => Beta1
                    [upgrade Type] => internal
                    [upgrade date] => 2019-04-10
                )

            [0] => Array
                (
                    [ppd name] => TOTO
                    [start clone] => 2019-04-24
                    [ppd delivery] => 2019-04-18
                    [target evenement] => lastBuild
                    [target date] => 2019-04-18
                )

            [1] => Array
                (
                    [ppd name] => TITI
                    [start clone] => 2019-04-05
                    [ppd delivery] => 2019-04-05
                    [target evenement] => lastBuild
                    [target date] => 2019-04-04
                )

        )

    [6] => Array
        (
            [id] => R420FD03Customers1556110346
            [main] => Array
                (
                    [release] => R420
                    [delivery] => FD03
                    [upgrade Type] => Customers
                    [upgrade date] => 2019-04-24
                )

        )

)

我尝试了

foreach ($arr_data as $Array) {
    foreach($Array as $subKey => $subArray) {
        if ($subArray['ppd name'] == 'TOTO') {
            unset($arr_data[$subKey]);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用array_walkforeach删除所需的元素

$search = 'TOTO';
array_walk($arr, function($value, $key) use (&$arr, $search){
    foreach($value as $k => $v){
        if(isset($v['ppd name']) && $v['ppd name'] == $search){

            unset($arr[$key][$k]);
        }
    }
});