【发布时间】: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", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "") );