У меня есть следующий массив:Может ли это быть возможной ошибкой php?
$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 пустых ячеек в ряд.
Кто-нибудь, кто понимает, в чем проблема?
Я бы ответил +1 на этот 100x, если бы мог, сначала сфотографировать код – zanderwar
Почему вы делаете 2D-петли? Массивы, похоже, имеют только 1 размер ... – Damiano
Назовите этот вопрос: Нет, это не ошибка PHP. – Simba