如果[conditions] index没有null,则删除数组中的数组

时间:2014-06-11 11:05:59

标签: php arrays multidimensional-array

这是数组输出

Array
(
    [conditions] => Array
        (
            [Service.organization_size_id] => Array
                (
                    [0] => 1
                )

        )

    [limit] => 10
    [joins] => Array
        (
            [0] => Array
                (
                    [type] => inner
                    [conditions] => Array
                        (
                            [ServicesToOrganizationTypes.organization_type_id] => Array
                                (
                                    [1] => 1
                                )

                            [0] => ServicesToOrganizationTypes.service_id = Service.id
                        )

                )

            [1] => Array
                (
                    [type] => LEFT
                    [conditions] => Array
                        (
                            [ServicesToLifeCycles.life_cycle_id] => Array
                                (
                                    [1] => 1
                                )

                            [0] => ServicesToLifeCycles.service_id = Service.id
                        )

                )

            [3] => Array
                (
                    [type] => inner
                    [conditions] => Array
                        (
                            [servicestoindustries.industry_id] => 
                            [0] => servicestoindustries.service_id = Service.id
                        )

                )
        )

    [group] => Service.id
)

如果[条件]在

下面没有值,我想删除一个数组部分
[3] => Array
                   (
                       [type] => inner
                       [conditions] => Array
                           (
                               [servicestoindustries.industry_id] => // here has no value 
                               [0] => servicestoindustries.service_id = Service.id
                           )

                   )

所以我想从数组

中删除它
[3] => Array
                   (
                       [type] => inner
                       [conditions] => Array
                           (
                               [servicestoindustries.industry_id] =>                      // here has no value 
                               [0] => servicestoindustries.service_id = Service.id
                           )

                   )

1 个答案:

答案 0 :(得分:0)

你可以尝试这个(未经测试)

<?php
/**
 * A redundant function to clean array
 */
function clean_array(&$array) {
    foreach($array as $key => $value) {
        if(is_array($value)) {
            clean_array($value); // If array, call the function itself
        } else if(empty($value)) {
            // Two choices
            unset($array[$key]); // Delete the array part
            unset($array) // Delete the whole array
        }
    }
}