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

如何从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.

您已在此列中创建了观察点1,2,3,4,5 ..... 100 将来 p>

示例:1,2,3,5,34,56,100 从3以上缺少此数字应该返回 p>

  $ watchPoints = $ videoWatchedData ['watch_points']; 
 $ fetArray = explode(“,”,$ watchPoints);  // unsorted 2,4,5,100,56,1,34 
  code>  pre> 
 
 

我想对上面的1,2,4,5,34进行排序, 56,100并返回第一个缺失的数字。 p>

我尝试过: p>

  $ sortFetchedArraysort = sort($ fetArray);  // ksort,rosrt没人工作
 
 
 $ Expected = 1; 
foreach($ sortFetchedArraysort as $ Number){
 if($ Expected!= $ Number){
 break; 
} \  n $预期++; 
} 
 $ percentageCount = $ Number;  //在我的案例输出中第一个缺少的数字应该返回3 
exit; 
  code>  pre> 
 
 

我遇到的两个问题是排序不工作第二个缺失 数字不是trturning。 p> blockquote> div>

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;

$watchPoints = "10,1,2,4,5,6,25,36,75,100";
$fetArray = explode(",", $watchPoints);
sort($fetArray);
for ($i = 0; $i < sizeof($fetArray); $i++) {
 if ($fetArray[$i] != $i + 1) {
    $missing = $i + 1;
    break;
 }
}
print($missing);

Try this few code, check the live demo.

<?php
  sort($array = explode(',', "10,1,2,4,5,6,25,36,75,100"));
  print_r(current(array_diff(range(1, 100), $array)));