PHP Notice: Undefined offset: 1 with array when reading data

前端 未结 10 1457
遇见更好的自我
遇见更好的自我 2020-12-04 15:14

I am getting this PHP error:

PHP Notice:  Undefined offset: 1

Here is the PHP code that throws it:

$file_handle = fopen($pa         


        
相关标签:
10条回答
  • 2020-12-04 15:34

    my quickest solution was to minus 1 to the length of the array as

      $len = count($data);
    
        for($i=1; $i<=$len-1;$i++){
          echo $data[$i];
        }
    

    my offset was always the last value if the count was 140 then it will say offset 140 but after using the minus 1 everything was fine

    0 讨论(0)
  • 2020-12-04 15:38

    Change

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

    to

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

    or simply:

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

    Not every line of your file has a colon in it and therefore explode on it returns an array of size 1.

    According to 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 讨论(0)
  • 2020-12-04 15:39

    The ideal solution would be as below. You won't miss the values from 0 to n.

    $len=count($data);
    for($i=0;$i<$len;$i++)
         echo $data[$i]. "<br>";
    
    0 讨论(0)
  • 2020-12-04 15:44

    How to reproduce the above error 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
    

    What does that error message mean?

    It means the php compiler looked for the key 1 and ran the hash against it and didn't find any value associated with it then said Undefined offset: 1

    How do I make that error go away?

    Ask the array if the key exists before returning its value like this:

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

    If the array does not contain your key, don't ask for its value. Although this solution makes double-work for your program to "check if it's there" and then "go get it".

    Alternative solution that's faster:

    If getting a missing key is an exceptional circumstance caused by an error, it's faster to just get the value (as in echo $yarr[1];), and catch that offset error and handle it like this: https://stackoverflow.com/a/5373824/445131

    0 讨论(0)
  • 2020-12-04 15:45

    Hide php warnings in file

    error_reporting(0);
    
    0 讨论(0)
  • 2020-12-04 15:54

    I just recently had this issue and I didn't even believe it was my mistype:

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

    And actually it was! I just accidentally typed "." rather than ","...

    0 讨论(0)
提交回复
热议问题