I have those few lines of code:
$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!
Now I will be using this piece of code alot in my code and I don't want to repeat it every time! I have tried to do this:
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;
}
And calling it like this: filter_array($array_to_filter, $filtered_array);
Without any luck.. What am I doing wrong?
The array I want to filter:
[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] =>
)
The result via the block of code:
[0] => Array
(
[RES_ID] => 2927135
[CONFIRMATION] => QBMNMA
[AIRBNB_AGENCY_INCOME] => €497
[AIRBNB_INCOME] => €516
[AIRBNB_FEES] => -€19
[AIRBNB_PER_NIGHT] => €129
)
The result via the function:
<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>
Would this work?