dongqiong8021 2017-05-13 07:14
浏览 93
已采纳

如何从php中的数组中获得第一个缺失数字100

hi i have created watch points in this columns 1,2,3,4,5.....100 will come

Example: 1,2,4,5,34,56,100 from above 3 is missing first this number should return

$watchPoints            =   $videoWatchedData['watch_points'];
$fetArray = explode(",",$watchPoints);  //unsorted 2,4,5,100,56,1,34

i want to sort the above one like this 1,2,4,5,34,56,100 and return first missing number.

What i have tried:

$sortFetchedArraysort   =   sort($fetArray ); //ksort,rosrt no one is working


$Expected = 1;
foreach ($sortFetchedArraysort as  $Number){
    if ($Expected != $Number)   {
        break;
    }
    $Expected++;
}
$percentageCount = $Number; // first missing number in my case output should return 3
exit;

Two problem i am facing one is sort not working second first missing number is not trturning.

  • 写回答

3条回答 默认 最新

  • duanqiang2617 2017-05-13 07:25
    关注

    Hope this simple one, will be helpful for you. In your post you are sorting $fetArray but there is no need, you can check it like this.

    <?php
    
    ini_set('display_errors', 1);
    $array=range(1,100);//your columns
    
    //you should sort like this, but it is not at all required
    $fetArray=array(2,4,5,100,56,1,34);
    sort($fetArray);
    
    //looping over array in which we are trying to find
    foreach($array as $value)
    {
        //at the moment your that value is not present in array we will break from loop
        if(!in_array($value, $fetArray))
        {
            break;
        }
    }
    //at the moment we break from loop we will get the value which is not present
    echo $value;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码