StackMaster
StackMaster

Reputation: 389

Could this be a possible php bug?

I have the following array:

$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", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "")
);

I want to check whether there's 3 empty cells, in a row, in each and every one of these arrays.

As the first cell of the array contains the name of the student, and the following cells are the ones who should be determined, I wrote this function (which starts at cell 1 of each array):

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;
}

But it doesn't seem to work, it seems there's a problem with the condition itself, because it seems it always return true (the if statement), because it gets to the array_push part if there are no 3 empty cells in a row.

Somebody who realizes what the problem is?

Upvotes: 1

Views: 150

Answers (2)

Ryan Vincent
Ryan Vincent

Reputation: 4513

There were no PHP bugs found.

What:

  • The student list is an array where each entry is for one student.

  • Each student entry is an array with the first entry as the name followed by a list of grades.

  • Find if there are three consecutive empty grades in an array of grades.

How:

  • Have a function (checkEmptySpaces) that receives an array of grades and return the position of the first of three consecutive empty grades. It returns -1 if there are not three consecutive empty grades.

  • call this function for each student in turn.

  • output: array of student name and position of first of three empty grades.

Demonstration at eval.in

Code:

/**
* 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;
}

run it:

$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;

Example Output:

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

Upvotes: 2

DamianoPantani
DamianoPantani

Reputation: 1376

Your arrays have one dimension only, so you don't need to nest a second loop inside this one. Am I right or missed something?

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

EDIT:

Yup I missed something - you didn't open a brace in the first line, so you DO have 2D array. Then, your code should look like follows:

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;
       }
    }
}

Change 0 to $i in the second line, $j+=3 to $j++, and add -2 to loop condition,

Upvotes: 0

Related Questions