我有以下数组:
$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语句),因为如果没有,它会到达array_push
部分连续3个空单元格。
有谁意识到问题所在?
答案 0 :(得分:2)
找不到PHP错误。
什么:
学生列表是一个数组,其中每个条目适用于一名学生。
每个学生条目都是一个数组,第一个条目作为名称,后跟成绩列表。
查找成绩数组中是否有三个连续的空成绩。
如何:
有一个函数(checkEmptySpaces
)接收一系列成绩并返回三个连续空成绩中的第一个的位置。如果没有连续三个空成绩,则返回-1。
依次为每个学生调用此功能。
输出:学生姓名数组和三个空成绩中第一个的位置。
代码:
/**
* 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
答案 1 :(得分:0)
您的数组只有一个维度,因此您不需要在此数组中嵌套第二个循环。我是对的还是错过了什么?
for($i=1; $i<count($arr)-2; $i++){
if(($arr[$i][$j] == "") && ($arr[$i][$j+1] == "") && ($arr[$i][$j+2] == "")){
$emptyThree = true;
}
}
修改强>:
是的,我错过了一些东西 - 你没有在第一行打开支架,所以你有2D阵列。然后,您的代码应如下所示:
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 添加到循环条件,