Think before you speak, read before you think.

php array_filter对数组进行过滤

by

in

将一个数组中能被 3 整除的数保留下来,需要写回调函数,满足条件的返回 true,否则返回 false

    $arr = array(1,261,262,263,264,265,266,267,268,269,270,271,272,273,275,274,276,279,277,280,278,281,282,287,283,288,289);
    function myFun($var){
        if($var%3==0)
            return true;
        else
            return false;
    }

    $filtered = array_filter($arr,"myFun");
    print_r($filtered); 

上面针对的是 value, 可以只针对 key 或针对 key, value 进行过滤

http://php.net/manual/en/function.array-filter.php


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *