将代码块转换为函数
Converting Block of code Into Function
我有那几行代码:
$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_array($array_to_filter, $filtered_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>
I have an Associative array but some of the arrays are empty! inside for example the [RES_ID] => I want to get rid of this whole array not just the key. that's what it does – Valery 5 mins ago
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);
这行得通吗?
我有那几行代码:
$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_array($array_to_filter, $filtered_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>
I have an Associative array but some of the arrays are empty! inside for example the [RES_ID] => I want to get rid of this whole array not just the key. that's what it does – Valery 5 mins ago
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);
这行得通吗?