Php 将代码块转换为函数

Php 将代码块转换为函数,php,mysql,codeigniter,php-5.3,Php,Mysql,Codeigniter,Php 5.3,我有以下几行代码: $array_to_filter // this array is unfiltered array_filter($array_to_filter, function($v){return array_filter($v) == array();}); $c = function($v){ return array_filter($v) != array(); }; $filtered_array = array_fi

我有以下几行代码:

        $array_to_filter // this array is unfiltered

   array_filter($array_to_filter, function($v){return array_filter($v) == array();});
    $c = function($v){
    return array_filter($v) != array();
    };
    $filtered_array = array_filter($airbnb, $c);


    print_r($filtered_array); // this is filtered array!
现在我将在我的代码中大量使用这段代码,我不想每次都重复它!我尝试过这样做:

    function filter_array ($tofilter, $filtered) {

    array_filter($tofilter, function($v){return array_filter($v) == array();});
    $c = function($v){
    return array_filter($v) != array();
    };
    $filtered = array_filter($filtered, $c); 
    return $filtered;
}
并这样称呼它:filter\u array($array\u to\u filter,$filter\u array)

没有任何运气。。我做错了什么

要筛选的阵列:

    [0] => Array
    (
        [RES_ID] => 2927135
        [CONFIRMATION] =>  QBMNMA
        [AIRBNB_AGENCY_INCOME] =>  €497
        [AIRBNB_INCOME] =>  €516

        [AIRBNB_FEES] =>  -€19

        [AIRBNB_PER_NIGHT] =>  €129
    )

[1] => Array
    (
        [RES_ID] => 
        [CONFIRMATION] => 
        [AIRBNB_AGENCY_INCOME] => 
        [AIRBNB_INCOME] => 
        [AIRBNB_FEES] => 
        [AIRBNB_PER_NIGHT] => 
    )
通过代码块得到的结果:

 [0] => Array
    (
        [RES_ID] => 2927135
        [CONFIRMATION] =>  QBMNMA
        [AIRBNB_AGENCY_INCOME] =>  €497
        [AIRBNB_INCOME] =>  €516

        [AIRBNB_FEES] =>  -€19

        [AIRBNB_PER_NIGHT] =>  €129
    )
通过该函数得到的结果:

<p>Severity: Warning</p>
<p>Message:  array_filter() expects parameter 1 to be array, null given</p>
<p>Filename: controllers/Welcome.php</p>
<p>Line Number: 456</p>
严重性:警告

消息:array_filter()要求参数1为array,给定null

文件名:controllers/Welcome.php

电话号码:456

我有一个关联数组,但有些数组是空的!例如,在[RES_ID]=>中,我想去掉整个数组,而不仅仅是键。这就是它所做的——瓦莱里5分钟前


这行吗?

定义为“没有运气”。它怎么会失败?嗯,它被使用了吗?您在哪里/如何使用它?当您调试它时,运行时的值究竟在哪里会变得意外?仅供参考,您对
array\u filter
的第一次调用完全没有任何作用。该函数应该做什么。用文字解释。我以为您要筛选的数组是
$array\u to\u filter
。。。这太令人困惑了。
function filter_array ($to_filter)
{
    foreach ($to_filter as $index => $child_array)
    {
        if (!array_filter($child_array)) {
            // all keys are empty.
            unset($to_filter[$index]);
        }
    }

return $to_filter;
}

$filtered_array = filter_array($array_to_filter);