【问题标题】:Could this be a possible php bug?这可能是一个可能的php错误吗?
【发布时间】:2016-09-02 09:23:01
【问题描述】:

我有以下数组:

$arr_nav=array(  
    array("Jimmy", "B", "C", "A", "B", "D", "A", "B", "C", "A", "D", "C", "A", "B", "C", "A", "B", "A", "D", "B", "C", "A"),
    array("John", "B", "", "", "A", "B", "C", "", "D", "", "", "", "", "", "", "", "", "", "", "", "", ""),
    array("George", "B", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "")
);

我想检查每个数组中是否有 3 个连续的空单元格。

由于数组的第一个单元格包含学生的姓名,而以下单元格是应该确定的单元格,我编写了这个函数(从每个数组的单元格 1 开始):

public function checkEmptySpaces($arr){

    $emptyThree = false; //A variable to store the current condition of three spaces in a row or not
    $emptyNames = array(); //Array of names, to return

    //Start going over the two dimensional array
    for($i=0; $i<count($arr); $i++){
        //Start from cell 1, and not 0, as cell 1 contains the name of the student, jump by 3 each time
        for($j=1; $j<count($arr[0]); $j+=3){ 
            //Check whether the current cell, the one that comes after, and then one after both of them are all empty
            if(($arr[$i][$j] == "") && ($arr[$i][$j+1] == "") && ($arr[$i][$j+2] == "")){
                //If yes, set $emptyThree to true
                $emptyThree = true;
            }
        }
        //Check if $emptyThree is set to true
        if($emptyThree == true){
            //If yes, push the name of the array's student (as stored in it's first cell), in the $emptyNames array which we return in the end of the function
            array_push($emptyNames, $arr[$i][0]);
        }
        //Reset the $emptyThree variable as the loops start going over the next array
        $emptyThree = false;
    }
    //Return the new array which contains the names of the students that has 3 spaces in a row in their arrays
    return $emptyNames;
}

但它似乎不起作用,条件本身似乎有问题,因为它似乎总是返回 true(if 语句),因为如果没有 3 空,它会到达 array_push 部分连续的单元格。

谁知道问题出在哪里?

【问题讨论】:

  • 如果我可以首先对代码进行去混淆处理,我会 +1 这个 100 倍的答案
  • 为什么要制作二维循环?数组似乎只有一维...
  • 这个问题的标题是:不,这不是 PHP 错误。
  • 如果您有很多复杂的代码,那么不,这可能不是 PHP 错误,而是您的复杂代码中的错误。 PHP 错误应该在 1-3 行代码中展示出来。
  • 打错了,应该是:$arr_nav=array( array("Jimmy", "B", "C", "A", "B", "D", "A", "B", "C", "A", "D", "C", "A", "B", "C", "A", "B", "A", "D", "B", "C", "A"), array("John", "B", "", "", "A", "B", "C", "", "D", "", "", "", "", "", "", "", "", "", "", "", "", ""), array("George", "B", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "") );

标签: php arrays


【解决方案1】:

没有发现 PHP 错误。

什么:

  • 学生列表是一个数组,其中每个条目都针对一个学生。

  • 每个学生条目都是一个数组,第一个条目是名称,后跟一个成绩列表。

  • 查找成绩数组中是否有三个连续的空成绩。

如何:

  • 有一个函数 (checkEmptySpaces),它接收一个等级数组并返回三个连续空等级中第一个的位置。如果没有三个连续的空等级,则返回-1。

  • 依次为每个学生调用此函数。

  • 输出:三个空年级中第一个的学生姓名和位置数组。

Demonstration at eval.in

代码:

/**
* Get start position of three empty cells
* return -1 if all ok
* 
* @param array $grades
* 
* @return integer
*/
function checkEmptySpaces($grades)
{
    for ($pos = 0, $len = count($grades); $pos < $len - 2; $pos++ ) {
        $emptyThree =     $grades[$pos] == ""
                       && $grades[$pos + 1] == ""
                       && $grades[$pos + 2] == "";

        if ($emptyThree) {
            return $pos;
        }              
    }      
    return -1;
}

运行它:

$outThreeEmpty = array();

foreach ($arr_nav as $grades) {

    $name = $grades[0]; // get name 
    $emptyPosition = checkEmptySpaces(array_slice($grades, 1));

    $outThreeEmpty[] = array($name => $emptyPosition);
} 

var_dump($outThreeEmpty);
exit;

示例输出:

array (size=4)
  0 => 
    array (size=1)
      'Jimmy' => int -1
  1 => 
    array (size=1)
      'John' => int 8
  2 => 
    array (size=1)
      'George' => int 1
  3 => 
    array (size=1)
      'onlyLast' => int 18

【讨论】:

    【解决方案2】:

    你的数组只有一维,所以你不需要在这个里面嵌套第二个循环。我是对的还是错过了什么?

    for($i=1; $i<count($arr)-2; $i++){
       if(($arr[$i][$j] == "") && ($arr[$i][$j+1] == "") && ($arr[$i][$j+2] == "")){
           $emptyThree = true;
       }
    }
    

    编辑:

    是的,我错过了一些东西——你没有在第一行打开大括号,所以你有二维数组。然后,您的代码应如下所示:

    for($i=0; $i<count($arr); $i++){
        for($j=1; $j<count($arr[$i])-2; $j++){
           if(($arr[$j] == "") && ($arr[$j+1] == "") && ($arr[$j+2] == "")){
               $emptyThree = true;
           }
        }
    }
    

    将第二行中的0改为$i,$j+=3改为$j++,并在循环条件中添加-2,

    【讨论】:

    • 是的,这是我的错误,它是一个二维数组。
    • 问题是,我确实想每次向前跳 3 个单元格($j+3),因为我已经检查了前面的两个单元格。我使用 $arr[i][0] 来获取当前数组的学生姓名。
    • @StackMaster 向前跳 3 个单元格听起来不太好。重新考虑跳跃部分,因为您每 3 个单元格检查一次是否有连续的空格。例如单元格 2,3 和 4 是空的,但您检查了单元格 1,2,3,然后在 4,5,6 处跳转。
    猜你喜欢
    • 2021-08-29
    • 2011-05-12
    • 1970-01-01
    • 2018-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-12
    相关资源
    最近更新 更多