ด้วยสิ่งต่าง ๆ เหล่านี้จะเป็นการดีกว่าหากคุณมีความชัดเจนในสิ่งที่คุณต้องการและไม่ต้องการ
มันจะช่วยให้คนต่อไปที่จะไม่ถูกจับด้วยความประหลาดใจที่พฤติกรรมของarray_filter()โดยไม่ต้องโทรกลับ ตัวอย่างเช่นฉันลงเอยกับคำถามนี้เพราะฉันลืมถ้าarray_filter()ลบNULLหรือไม่ ฉันเสียเวลาเมื่อฉันสามารถใช้วิธีการแก้ปัญหาด้านล่างและได้รับคำตอบของฉัน
นอกจากนี้ตรรกะคือผู้ไม่เชื่อเรื่องภาษาในแง่ที่ว่ารหัสสามารถคัดลอกเป็นภาษาอื่นโดยไม่ต้องอยู่ภายใต้พฤติกรรมของฟังก์ชั่น PHP เช่นarray_filterเมื่อไม่มีการส่งกลับโทรกลับ
ในโซลูชันของฉันจะเห็นได้อย่างชัดเจนว่าเกิดอะไรขึ้น ลบเงื่อนไขเพื่อเก็บบางสิ่งหรือเพิ่มเงื่อนไขใหม่เพื่อกรองค่าเพิ่มเติม
ไม่ต้องสนใจการใช้งานจริง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);
ข้อดีอีกประการของวิธีนี้คือคุณสามารถแยกการกรองภาคแสดงออกเป็นฟังก์ชันนามธรรมที่กรองค่าเดียวต่ออาร์เรย์และสร้างโซลูชันแบบเรียงซ้อน
ดูตัวอย่างนี้และความคิดเห็นแบบอินไลน์สำหรับผลลัพธ์
<?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]
ตอนนี้คุณสามารถสร้างฟังก์ชั่นที่เรียกว่าการfilterer()ใช้แบบไดนามิกpipe()ซึ่งจะใช้ฟังก์ชั่นที่ใช้บางส่วนเหล่านี้สำหรับคุณ
<?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]