PHP Error Undefined offset: 2

1

When programming this code, the following error appears: Undefined offset: 2 . What could be the cause?

function checkCourt($fecha,$pista){
        //Entorno:
        $coincidencias;$fp;$nombre_archivo;$linea;
        //Algoritmo:
        $coincidencias=false;
        $nombre_archivo = "reservas.txt"; 
            file_exists($nombre_archivo);
            //Abrimos el handle
            $fp = fopen($nombre_archivo, "r");
            //Movemos el cursor al principio, por posible desajustes
            rewind($fp);
        while (!feof($fp) && !$coincidencias){

                $linea = fgets($fp);
                $linea=explode(" ", $linea);
                //strototime para convertirlo a date unix
                if(strtotime($linea[2])==strtotime($fecha)&&$linea[1]==$pista){

                    $coincidencias=true;

                }//Fin Si

        }//Fin Mientrasa
        fclose($fp);

            return $coincidencias;
    }//Fin Función 
    
asked by Wallx 27.10.2018 в 21:49
source

1 answer

1

I think the problem is on the line

if(strtotime($linea[2])...

Surely one of the lines of the file you are reading does not have two spaces, so, $ line is not created as a vector of 3 positions.

To avoid this you could modify that if for this other

if (count($linea) > 2 && strtotime($linea[2])==strtotime($fecha) && $linea[1]==$pista)...

Good luck!

    
answered by 27.10.2018 / 22:25
source