Home >Backend Development >PHP Tutorial >I wrote a function to filter the array. But a value is always missing. Can anyone help me?

I wrote a function to filter the array. But a value is always missing. Can anyone help me?

WBOY
WBOYOriginal
2016-12-01 00:56:551095browse

It is to filter the minimum and maximum values...
However, when
user_array_filter( $aTxt, 'After_A_Num' , $iMaxStamp);
, I found that a value equal to $iMaxStamp was missed. Can anyone help me? What's going on?
I can't see anything wrong...
I wrote a function to filter the array. But a value is always missing. Can anyone help me?

<code>
function user_array_filter( &$array, $callback )
//后面可以跟第3个参数,作为 $callback 的参数;
{
    $args = func_get_args();    //把function 所有参数保存成数组;


    if( count($args) < 2 )
        exit('function user_array_filter()参数错误 , 最少有两个参数.');


    if( count($args) == 2 )    //如果俩参数,就是有 $arr 和 $callback
    {
        foreach( $array as $k => $v )
        {
            $result = $callback($v);

            if( !$result )    //如果 function 返回 false ;
            {
                unset( $array[$k] );
            }
        }
    }



    else
    {
        $limit = $args[2];    // [0]->处理数组 , [1]->function , [2]->value

        foreach( $array as $k => $v )
        {
            $result = $callback($v, $limit);

            if( !$result )    //如果 function 返回 false ;
            {
                unset( $array[$k] );
            }
        }
    }

    $array = array_values($array);     //把键重定义;


}


    function Before_A_Num($num , $limit)
    {
        if ($num < $limit)
            return true;
        return false;
    }

    function After_A_Num($num , $limit)
    {
        if ($num > $limit)
            return true;
        return false;
    }
</code>

Reply content:

It is to filter the minimum and maximum values...
However, when
user_array_filter( $aTxt, 'After_A_Num' , $iMaxStamp);
, I found that a value equal to $iMaxStamp was missed. Can anyone help me? What's going on?
I can't see anything wrong...
I wrote a function to filter the array. But a value is always missing. Can anyone help me?

<code>
function user_array_filter( &$array, $callback )
//后面可以跟第3个参数,作为 $callback 的参数;
{
    $args = func_get_args();    //把function 所有参数保存成数组;


    if( count($args) < 2 )
        exit('function user_array_filter()参数错误 , 最少有两个参数.');


    if( count($args) == 2 )    //如果俩参数,就是有 $arr 和 $callback
    {
        foreach( $array as $k => $v )
        {
            $result = $callback($v);

            if( !$result )    //如果 function 返回 false ;
            {
                unset( $array[$k] );
            }
        }
    }



    else
    {
        $limit = $args[2];    // [0]->处理数组 , [1]->function , [2]->value

        foreach( $array as $k => $v )
        {
            $result = $callback($v, $limit);

            if( !$result )    //如果 function 返回 false ;
            {
                unset( $array[$k] );
            }
        }
    }

    $array = array_values($array);     //把键重定义;


}


    function Before_A_Num($num , $limit)
    {
        if ($num < $limit)
            return true;
        return false;
    }

    function After_A_Num($num , $limit)
    {
        if ($num > $limit)
            return true;
        return false;
    }
</code>

Let me be clear, your current problem (the following pseudo code) is if:

<code>$a = [1,3,45,6,6,7,7,5];
user_array_filter($a, 'After_A_Num', 6);
var_dump($a);    //目前的结果[7,7,45]</code>

But the result you expect is [6,6,7,7,45], is that right?
If this is the case, modify the After_A_Num function from greater than to greater than or equal to.

Also, I don’t think it’s good to name your function like this

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn