Avoid multiple PHP Notice: Undefined index: inside a loop for

Asked

Viewed 810 times

-1

The variable $words concatenates the result of a query into a column of a table where words are separated by comma.

Only that when running the script for each word inside the for is is generated a PHP Notice: Undefined index: agua in ....

If there are 50 words will be generated 50 PHP Notice: Undefined index: PALAVRA_DO_FOR in ...

$palavras = explode(',', $words);
$ocorrencias = array();

for($i = 0; $i<count($palavras); $i++){
   $palavra = $palavras[$i];
   $ocorrencias[$palavra]++;
}

How to avoid these PHP Notice: Undefined index: ...?

PHP Version 5.4.43

  • can I know why the negative? I should know how to solve this kind of doubt? I searched related and did not find

  • Checks if the key exists with isset and if it does not exist, create it before using it.

  • @Andersoncarloswoss did not understand how to do this, could post as response?

3 answers

3


The error occurs because PHP tries to access a key from array which may not exist initially. For example, in the first word, $ocorrencias is an empty list and when you do $ocorrencias[$palavra] you will be accessing a non-existent key. To circumvent the error, simply check if the key exists and, if it does not exist, create it with zero.

<?php

$words = "stack,overflow,em,portugûes";
$palavras = explode(',', $words);
$ocorrencias = array();

for($i = 0; $i < count($palavras); $i++){

    $palavra = $palavras[$i];

    // Se a chave não existir, crie-a com valor zero:
    if (!isset($ocorrencias[$palavra])) {
        $ocorrencias[$palavra] = 0;
    }

    $ocorrencias[$palavra]++;

}

print_r($ocorrencias);

See working on Ideone.

2

One way to solve this problem is to initialize the occurrence array with all elements already zeroed, with the function array_fill_keys() the first argument is the array that will be transformed into the output array keys, the second is the default value for all elements.

Then you can change the for for foreach and just increment the key.

$words = 'teste,abc,2015,teste,13,abc,2015';

$palavras = explode(',', $words);
$ocorrencias = array_fill_keys($palavras, 0);

foreach($palavras as $item){
    $ocorrencias[$item]++;
}

Return:

Array
(
    [teste] => 2
    [abc] => 2
    [2015] => 2
    [13] => 1
)

-3

I might as well suggest you to disable PHP warning messages in your script in this way:

ini_set('display_errors', FALSE);

But it would not be very convenient, because the most appropriate thing is to work your script to contain clean code, as it is better to correct the error than to hide it. To do this just use a PHP function (isset) to check if the index exists before trying to use it.

Example:

$palavras = explode(',', $words);
$ocorrencias = array();

for($i = 0; $i<count($palavras); $i++){
  if (isset($palavras[$i])) {
    $palavra = $palavras[$i];
    $ocorrencias[$palavra]++;
  }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.