Handler for undefined index in array

Asked

Viewed 99 times

1

Dear experienced developers and codar art learners.

I believe it is common in some system you have to check the next item of a array. For example in this case I am checking if the next one is the same as the previous one:

$item_anterior = "";
$array = [0,1,2,3,4,5,6,7,8,9,10];
$ainda_nao_foi = true;

for($i=0;$i<$array.lenght();$i++){   

   if($ainda_nao_foi){   
     echo "<p>O Site do Stack overflow é demais! .$array[$i].</p>";
     $item_anterior = $array[$i];
     $ainda_nao_foi = false;
   }
   //próximo
   $j = $i + 1;

   if($item_anterior != array[$j]){
        $ainda_nao_foi = true;
   }
}

Even using the function isset() to check if it is set, this would not help, because sometimes my array can return with empty value.

The problem is that in this if:

if($item_anterior != array[$j]){
   $ainda_nao_foi = true;
}

The PHP generates a warning warning that the index is not set. There is a function that checks whether you have this index in the array? not to blow the bug?

  • I understand you want to check if the next item is equal to the current one, but what is the expected output? You may have better alternatives to solve this.

  • You might give this message after $i reaches the last possible value and $j wants to go further, since its value is $j=$i+1. The highest rate is 10, in your case, but $j wants to go up to 10+1, which doesn’t exist.

  • 1

    If you only need to compare an item with the following because it does not simplify and change the for for < length - 1 ? So you don’t even need the previous one, just compare it directly with the next one, because you know it’s always valid

2 answers

2


To check if the key exists even if the value is null use the function array_key_exists() in place of isset() may attach this verification to the second if.

$item_anterior = "";
$array = [0,1,1,2,3,4,5,6,7,8,9,10, '', null, null];
$ainda_nao_foi = true;

foreach($array as $k => $v){

    if($ainda_nao_foi){
        echo "<p>O Site do Stack overflow é demais! .$array[$k].</p>";
        $item_anterior = $array[$k];
        $ainda_nao_foi = false;
    }

    $j = $k + 1;

    if(array_key_exists($j, $array) && $item_anterior != $array[$j]){
        $ainda_nao_foi = true;
    }
}

A simpler example of the different functions:

$arr = array(1, null, '' , 'teste');

var_dump(isset($arr[1])); //false
var_dump(array_key_exists(1, $arr)); //true

Example - ideone

Related:

What is the difference in checking an array with isset and array_key_exists?

0

I would do the opposite, instead of checking the next, I would check the previous:

for ($i=0; $i < $array.lenght(); $i++){   
   if(($i == 0) || ($array[$i - 1] != $array[$i])) 
     echo "<p>O Site do Stack overflow é demais! .$array[$i].</p>";
}

But if you want to keep it in this format, you could add a if:

$item_anterior = "";
$array = [0,1,2,3,4,5,6,7,8,9,10];
$ainda_nao_foi = true;

for($i=0;$i<$array.lenght();$i++){   

   if($ainda_nao_foi){   
     echo "<p>O Site do Stack overflow é demais! .$array[$i].</p>";
     $item_anterior = $array[$i];
     $ainda_nao_foi = false;
   }
   //próximo
   $j = $i + 1;

   if(($j < $array.lenght()) && ($item_anterior != array[$j])){
        $ainda_nao_foi = true;
   }
}

Browser other questions tagged

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