Notice: Undefined offset: 0 in

后端 未结 14 1551
太阳男子
太阳男子 2020-11-28 11:05

I am getting this PHP error, what does it mean?

Notice: Undefined offset: 0 in 
C:\\xampp\\htdocs\\mywebsite\\reddit_vote_tut\\src\\votes.php on line 41


        
相关标签:
14条回答
  • 2020-11-28 11:28

    As you might have already about knew the error. This is due to trying to access the empty array or trying to access the value of empty key of array. In my project, I am dealing with this error with counting the array and displaying result.

    You can do it like this:

    if(count($votes) == '0'){
    
        echo 'Sorry, no votes are available at the moment.';
    }
    else{
        //do the stuff with votes
    }
    

    count($votes) counts the $votes array. If it is equal to zero (0), you can display your custom message or redirect to certain page else you can do stuff with $votes. In this way you can remove the Notice: Undefined offset: 0 in notice in PHP.

    0 讨论(0)
  • 2020-11-28 11:32

    I encountered this as well and the solution is simple, dont hardcode the array index position in your code.
    Instead of $data[0]['somekey'] do foreach($data as $data_item) { echo $data_item['somekey']; }
    If there is an array or more you can perform your desired action inside the loop, but if it's undefined it will not bring an error. you can also add other checks on top of that. You could also add a variable and increment it in a for in loop to limit your looping if you want only the first positions or something.

    0 讨论(0)
  • 2020-11-28 11:34

    You are asking for the value at key 0 of $votes. It is an array that does not contain that key.

    The array $votes is not set, so when PHP is trying to access the key 0 of the array, it encounters an undefined offset for [0] and [1] and throws the error.

    If you have an array:

    $votes = array('1','2','3');
    

    We can now access:

    $votes[0];
    $votes[1];
    $votes[2];
    

    If we try and access:

    $votes[3];
    

    We will get the error "Notice: Undefined offset: 3"

    0 讨论(0)
  • 2020-11-28 11:34

    first, check that the array actually exists, you could try something like

    if (isset($votes)) {
       // Do bad things to the votes array
    }
    
    0 讨论(0)
  • 2020-11-28 11:35
    function getEffectiveVotes($id) 
    

    According to the function header, there is only one parameter variable ($id). Thus, on line 27, the votes[] array is undefined and out of scope. You need to add another parameter value to the function header so that function getEffectiveVotes() knows to expect two parameters. I'm rusty, but something like this would work.

    function getEffectiveVotes($id, $votes)
    

    I'm not saying this is how it should be done, but you might want to research how PHP passes its arrays and decide if you need to explicitly state to pass it by reference

    function getEffectiveVotes($id &$votes)    <---I forget, no time to look it up right now.
    

    Lastly, call function getEffectiveVotes() with both arguments wherever it is supposed to be called.

    Cheers.

    0 讨论(0)
  • 2020-11-28 11:37

    This answer helped me https://stackoverflow.com/a/18880670/1821607 The reason of crush — index 0 wasn't set. Simple $array = $array + array(null) did the trick. Or you should check whether array element on index 0 is set via isset($array[0]). The second variant is the best approach for me.

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