Array_filter and empty()

后端 未结 5 1374
梦毁少年i
梦毁少年i 2020-12-19 17:33

Warning: array_filter() expects parameter 2 to be a valid callback, function \'empty\' not found or invalid function name....

Why is e

相关标签:
5条回答
  • 2020-12-19 17:45

    I think a better solution exist and I found it reading the doc about array_filter

    See this comment : https://www.php.net/manual/fr/function.array-filter.php#111091

    <?php
    
    // removes all NULL, FALSE and Empty Strings but leaves 0 (zero) values
    $result = array_filter( $array, 'strlen' );
    
    ?>
    

    I only allow myself to post it because I think it's a very elegant solution.

    Also, as array_filter itself already filter null or false value as a default callback, this code work too :

    $array = array('apple', '', 'watermelon');
    var_dump(array_filter($array));
    
    ----
    Output : 
    array(2) { [0]=> string(5) "apple" [2]=> string(10) "watermelon" }
    
    0 讨论(0)
  • 2020-12-19 17:47

    Answer

    empty() is not a function but a language construct and array_filter() can only accept a function as its callback.

    This is given as a small note on the manual page:

    Note: Because this is a language construct and not a function, it cannot be called using variable functions

    Work around

    To work around this you can wrap empty in another function for example:

    function empty_test($val) {
        return empty($val);
    }
    

    And then call it like so:

    $arr = array_filter($arr, 'empty_test');
    
    0 讨论(0)
  • 2020-12-19 17:48

    empty() is a language construct, and not a true function in terms of PHP, so you can't pass its name as an argument to functions like array_filter() and call_user_func_array().

    From the manual:

    Note: Because this is a language construct and not a function, it cannot be called using variable functions

    For a workaround, just wrap it in another user-defined function; see Treffynnon's answer.

    0 讨论(0)
  • 2020-12-19 17:54

    See the documentation page on empty():

    Note: Because this is a language construct and not a function, it cannot be called using variable functions

    So basically empty() is not a function, and because callback must be a function, empty() can not be passed as callback.

    But you can create callback that may use empty(). The following should work in PHP > 5.3:

    $arr = array_filter($arr, function($var){
        return empty($var);
    });
    

    In PHP < 5.3 you will need to create similar function first and then pass it to the array_filter().

    Did it help?

    0 讨论(0)
  • 2020-12-19 18:05

    You can use just array_filter() function without callback:

    Remove empty array elements in PHP

    $arr = array("PHP", "HTML", "CSS", "", "JavaScript", null, 0);
    print_r(array_filter($arr)); // removing blank, null, false, 0 (zero) values
    

    Result:

    Array
    (
        [0] => PHP
        [1] => HTML
        [2] => CSS
        [4] => JavaScript
    )
    
    0 讨论(0)
提交回复
热议问题