如何在php中从数组中提取第一个缺失的数字100
您好,我已经在这一栏中创建了观察点1、2、3、4、5…..100将会出现 示例:1,2,4,5,34,56100 从上面的3开始,这个数字应该首先返回如何在php中从数组中提取第一个缺失的数字100,php,arrays,string,sorting,Php,Arrays,String,Sorting,您好,我已经在这一栏中创建了观察点1、2、3、4、5…..100将会出现 示例:1,2,4,5,34,56100 从上面的3开始,这个数字应该首先返回 $watchPoints = $videoWatchedData['watch_points']; $fetArray = explode(",",$watchPoints); //unsorted 2,4,5,100,56,1,34 我想对上面的1,2,4,5,34,56100进行排序,并返回第一个缺失的数字 我所
$watchPoints = $videoWatchedData['watch_points'];
$fetArray = explode(",",$watchPoints); //unsorted 2,4,5,100,56,1,34
我想对上面的1,2,4,5,34,56100进行排序,并返回第一个缺失的数字
我所尝试的:
$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;
我面临的两个问题一个是排序不工作,第二个是第一个丢失的号码不正确
希望这个简单的,会对你有所帮助。在您的帖子中,您正在排序
$fetArray
,但是没有必要,您可以这样检查它
<?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;
希望这篇简单的文章对您有所帮助。在您的帖子中,您正在排序$fetArray
,但是没有必要,您可以这样检查它
<?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,75100”;
$FETRARY=爆炸(“,”,$watchPoints);
排序(数组);
对于($i=0;$i
$watchPoints=“10,1,2,4,5,6,25,36,75100”;
$FETRARY=爆炸(“,”,$watchPoints);
排序(数组);
对于($i=0;$i
试试这几个代码,检查
<?php
sort($array = explode(',', "10,1,2,4,5,6,25,36,75,100"));
print_r(current(array_diff(range(1, 100), $array)));
试试这几个代码,检查
<?php
sort($array = explode(',', "10,1,2,4,5,6,25,36,75,100"));
print_r(current(array_diff(range(1, 100), $array)));
请定义“sort not working”。结果是什么?sort不返回数组,它在数组上运行@KIKOSoftware那么我现在的解决方案是什么呢代码>仅排序($sortFetchedArraysort)代码>替换行$sortFetchedArraysort=sort($sortFetchedArraysort)代码>按排序($sortFetchedArraysort)
如果$sortFetchedArraysort
包含您的数组,而不是您问题中的$fetArray
。请定义“排序不起作用”。结果是什么?排序不返回数组,它在数组上运行@KIKOSoftware那么我现在的解决方案是什么呢代码>仅排序($sortFetchedArraysort)代码>替换行$sortFetchedArraysort=sort($sortFetchedArraysort)代码>按排序($sortFetchedArraysort)
如果$sortFetchedArraysort
包含您的数组,而不是像您所问的那样$fetArray
。array_diff($array,$fetArray)@JYoThI你能用这个给examle吗answer@SahilGulati你能告诉我第一个丢失的号码是怎么触发的吗。如果数组是这样的$fetArray=array(72,4,5100,56,1,34)代码>那么第一个输出是什么?@AbdulWaheed你能检查一下吗?希望这能帮助你。array_diff($array,$fetArray)@JYoThI你能用这个给examle吗answer@SahilGulati你能告诉我第一个丢失的号码是怎么触发的吗。如果数组是这样的$fetArray=array(72,4,5100,56,1,34)代码>那么第一次发布的是什么?@AbdulWaheed你能检查一下吗?希望这能帮助你。