drfif48428 2018-06-08 19:36
浏览 14
已采纳

通过添加一个直到找到针头在大海捞针中找到针头

I have this array of random numbers created by a table in database

$numbersArr = array(3,5,17,19,24,30,38,...)
$userInput = 35;

So far this is what I have

if (in_array($userInput, $numbersArr)){

 echo 'found needle';

}else{

//add one to the $userInput until find a needle

}

So the program would add 1 to 35 until it found 38, then stop. How can I do this? I'm pretty sure it's straightforward but I cant wrap my head around it.

  • 写回答

5条回答 默认 最新

  • dongshi6710 2018-06-08 20:10
    关注

    There is no need for looping.
    You can filter the array, and echo min number.

    $numbersArr = array(3,5,17,19,24,30,38,12,15,21,67);
    $userInput = 35;
    
    // Filter array, return values larger than user input
    $filtered = array_filter($numbersArr,
        function ($value) use($userInput) {
            return $value >= $userInput;
        }
    );
    If(count($filtered)>0){
        // Echo smallest number in array
        Echo min($filtered);
    }else{
        Echo "no number larger than user input";
    }
    

    Try it here: https://3v4l.org/fMg7K

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备