How to find missing values in a sequence with PHP?

后端 未结 3 968
甜味超标
甜味超标 2021-01-21 14:02

Suppose you have an array \"value => timestamp\". The values are increasing with the time but they can be reset at any moment.

For example :

$array = arr         


        
3条回答
  •  Happy的楠姐
    2021-01-21 14:58

    This gives the desired result array(4,5,6,58):

    $previous_value = NULL;
    $temp_store = array();
    $missing = array();
    $keys = array_keys($array);
    
    for($i = min($keys); $i <= max($keys); $i++)
    {
        if(!array_key_exists($i, $array))
        {
            $temp_store[] = $i;
        }
        else
        {
            if($previous_value < $array[$i])
            {
                $missing = array_merge($missing, $temp_store);
            }
            $temp_store = array();
            $previous_value = $array[$i];
        }
    }
    
    var_dump($missing);
    

    Or just use Gumbo's very smart solution ;-)

提交回复
热议问题