dtsc14683 2018-04-11 06:32
浏览 35
已采纳

获取for循环中不存在的数字

I have 2 integers.

$a = 5;
$b = 3;

This is my code as of now, I want to do it vice versa, which is to get the integer that does not exist. Instead of getting the existing numbers which are 1,2,3. I would like to execute a command on the numbers that does not exist (4 & 5).

for ($x = 1; $x <= $a; $x++) {
    for ($y = 1; $y <= $b; $y++) {
        if ($x == $y)
        {
            echo $y." = Exist Do some commands here<br>";
        }
    }
}
  • 写回答

2条回答 默认 最新

  • doukan5332 2018-04-11 06:47
    关注

    You can use the following solution:

    <?php
    $a = 3;
    $b = 5;
    
    $arrNotExists = [];
    
    for ($i = $a + 1; $i <= $b; $i++) {
        $arrNotExists[] = $i;
    }
    
    var_dump($arrNotExists);
    

    demo: https://ideone.com/bOSsH8

    Another solution using two arrays with array_diff:

    <?php
    $a = 3;
    $b = 5;
    
    $arrA = [];
    $arrB = [];
    
    for ($i = 1; $i <= $a; $i++) {
        $arrA[] = $i;
    }
    
    for ($i = 1; $i <= $b; $i++) {
        $arrB[] = $i;
    }
    
    $arrNotExists = array_diff($arrB, $arrA);
    
    var_dump($arrNotExists);
    

    demo: https://ideone.com/OtSwnN

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

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建