2013-07-03 15 views
39

sto ottenendo questo errore PHP:PHP Avviso: undefined offset: 1 con l'allineamento durante la lettura dei dati

PHP Notice: Undefined offset: 1 

Ecco il codice PHP che getta:

$file_handle = fopen($path."/Summary/data.txt","r"); //open text file 
$data = array(); // create new array map 

while (!feof($file_handle)) { 
    $line_of_text = fgets($file_handle); // read in each line 
    $parts = array_map('trim', explode(':', $line_of_text, 2)); 
    // separates line_of_text by ':' trim strings for extra space 
    $data[$parts[0]] = $parts[1]; 
    // map the resulting parts into array 
    //$results('NAME_BEFORE_:') = VALUE_AFTER_: 
} 

Che cosa significa questo errore ? Cosa causa questo errore?

+1

Come sicuri sono che ogni riga nel file ha due punti in esso? – andrewsi

+0

fare un 'var_dump ($ parti)'. probabilmente troverai il punto in cui ottieni quell'offset non definito, non c'è la chiave '1' nell'array delle parti. –

+2

Se sembra che tutte le linee abbiano due punti, controllare le righe vuote. – Barmar

risposta

76

Change

$data[$parts[0]] = $parts[1]; 

a

if (! isset($parts[1])) { 
    $parts[1] = null; 
} 

$data[$parts[0]] = $parts[1]; 

o semplicemente:

$data[$parts[0]] = isset($parts[1]) ? $parts[1] : null; 

Non ogni riga del file ha due punti in esso e quindi esplodere su di esso restituisce un array di dimensione 1.

Secondo php.net possible return values from explode:

Returns an array of strings created by splitting the string parameter on boundaries formed by the delimiter.

If delimiter is an empty string (""), explode() will return FALSE. If delimiter contains a value that is not contained in string and a negative limit is used, then an empty array will be returned, otherwise an array containing string will be returned.

+0

Grazie GGio! Ottima spiegazione, molto chiara. – alchuang

+0

Funziona. Grazie –

1

Si tratta di un "PHP Notice", quindi in teoria si può ignorarlo. Cambia php.ini:

error_reporting = E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED 

Per

error_reporting = E_ALL & ~E_NOTICE 

Questo spettacolo tutti gli errori, ad eccezione di avvisi.

17

come riprodurre l'errore precedente in PHP:

php> $yarr = array(3 => 'c', 4 => 'd'); 

php> echo $yarr[4]; 
d 

php> echo $yarr[1]; 
PHP Notice: Undefined offset: 1 in 
/usr/local/lib/python2.7/dist-packages/phpsh/phpsh.php(578) : 
eval()'d code on line 1 

Che cosa significa che il messaggio di errore?

Significa che il compilatore php cercato la chiave 1 e corse l'hash contro di essa e non ha trovato alcun valore ad essa associato ha poi detto Undefined offset: 1

Come faccio a fare questo errore andare via?

Chiedere la matrice, se esiste la chiave prima di tornare il suo valore in questo modo:

php> echo array_key_exists(1, $yarr); 

php> echo array_key_exists(4, $yarr); 
1 

Se la matrice non contiene la chiave, non chiedere per il suo valore. Anche se questa soluzione rende il doppio lavoro per il tuo programma "controlla se è lì" e poi "vai a prenderlo".

soluzione alternativa che è più veloce:

Se ottenere una chiave mancante è una circostanza eccezionale causata da un errore, è più veloce per ottenere solo il valore (come in echo $yarr[1];), e cattura che compensato l'errore e gestirlo così: https://stackoverflow.com/a/5373824/445131

-5

puoi anche provare a rimuovere avviso ...

error_reporting=0 
0

Ho da poco avuto questo problema e non ho nemmeno credere che era il mio mistype:

Array("Semester has been set as active!", true) 
Array("Failed to set semester as active!". false) 

E in realtà è stato! Ho appena digitato per sbaglio "." piuttosto che "," ...

Problemi correlati