有了这些类型的东西,最好明确说明你想要什么和不想要什么。
这将有助于下一个人不会在没有回调的情况下对 array_filter() 的行为感到惊讶。例如,我最终回答了这个问题,因为我忘记了array_filter() 是否删除了NULL。我本可以使用下面的解决方案并得到答案,但我浪费了时间。
此外,从某种意义上说,该逻辑与语言无关,即可以将代码复制到另一种语言中,而无需了解没有传递回调时像 array_filter 这样的 php 函数的行为。
在我的解决方案中,发生的事情一目了然。删除条件以保留某些内容或添加新条件以过滤其他值。
忽略array_filter() 的实际使用,因为我只是向它传递了一个自定义回调 - 如果你愿意,你可以继续将它提取到它自己的函数中。我只是将它用作foreach 循环的糖。
<?php
$xs = [0, 1, 2, 3, "0", "", false, null];
$xs = array_filter($xs, function($x) {
if ($x === null) { return false; }
if ($x === false) { return false; }
if ($x === "") { return false; }
if ($x === "0") { return false; }
return true;
});
$xs = array_values($xs); // reindex array
echo "<pre>";
var_export($xs);
这种方法的另一个好处是,您可以将过滤谓词分解为一个抽象函数,该函数过滤每个数组的单个值并构建一个可组合的解决方案。
请参阅此示例和输出的内联 cmets。
<?php
/**
* @param string $valueToFilter
*
* @return \Closure A function that expects a 1d array and returns an array
* filtered of values matching $valueToFilter.
*/
function filterValue($valueToFilter)
{
return function($xs) use ($valueToFilter) {
return array_filter($xs, function($x) use ($valueToFilter) {
return $x !== $valueToFilter;
});
};
}
// partially applied functions that each expect a 1d array of values
$filterNull = filterValue(null);
$filterFalse = filterValue(false);
$filterZeroString = filterValue("0");
$filterEmptyString = filterValue("");
$xs = [0, 1, 2, 3, null, false, "0", ""];
$xs = $filterNull($xs); //=> [0, 1, 2, 3, false, "0", ""]
$xs = $filterFalse($xs); //=> [0, 1, 2, 3, "0", ""]
$xs = $filterZeroString($xs); //=> [0, 1, 2, 3, ""]
$xs = $filterEmptyString($xs); //=> [0, 1, 2, 3]
echo "<pre>";
var_export($xs); //=> [0, 1, 2, 3]
现在您可以使用 pipe() 动态创建一个名为 filterer() 的函数,该函数将为您应用这些部分应用的函数。
<?php
/**
* Supply between 1..n functions each with an arity of 1 (that is, accepts
* one and only one argument). Versions prior to php 5.6 do not have the
* variadic operator `...` and as such require the use of `func_get_args()` to
* obtain the comma-delimited list of expressions provided via the argument
* list on function call.
*
* Example - Call the function `pipe()` like:
*
* pipe ($addOne, $multiplyByTwo);
*
* @return closure
*/
function pipe()
{
$functions = func_get_args(); // an array of callable functions [$addOne, $multiplyByTwo]
return function ($initialAccumulator) use ($functions) { // return a function with an arity of 1
return array_reduce( // chain the supplied `$arg` value through each function in the list of functions
$functions, // an array of functions to reduce over the supplied `$arg` value
function ($accumulator, $currFn) { // the reducer (a reducing function)
return $currFn($accumulator);
},
$initialAccumulator
);
};
}
/**
* @param string $valueToFilter
*
* @return \Closure A function that expects a 1d array and returns an array
* filtered of values matching $valueToFilter.
*/
function filterValue($valueToFilter)
{
return function($xs) use ($valueToFilter) {
return array_filter($xs, function($x) use ($valueToFilter) {
return $x !== $valueToFilter;
});
};
}
$filterer = pipe(
filterValue(null),
filterValue(false),
filterValue("0"),
filterValue("")
);
$xs = [0, 1, 2, 3, null, false, "0", ""];
$xs = $filterer($xs);
echo "<pre>";
var_export($xs); //=> [0, 1, 2, 3]