1
Colleagues, sometimes I come across some snippets in the code used the variable $i before the repeat loop ($i = 0) and in the middle and end $i++, as the examples below:
Ex1)
$c = count($obj);
$i = 0; //aqui
for ($j = 0; $j < $c; $j++) {
$matr = $obj[$j]->MATR;
$nome = trim($obj[$j]->NOME);
$cd_lot = trim($obj[$j]->LOTACAO);
$nm_lot = $obj[$j]->DESC_LOTA;
$regional = $obj[$j]->ESTAB;
$cargo = $obj[$j]->CARGO;
if (($isAtivo) && (substr($cd_lot, 0, 1) == 'E') ) {
$arrFolhaOrd[$i] = array();
$arrFolhaOrd[$i]['nome'] = $nome;
$arrFolhaOrd[$i]['matr'] = $matr;
$arrFolhaOrd[$i]['cargo'] = $cargo;
$i++; //aqui
$arrFolha1[$matr] = array();
$arrFolha1[$matr]['nome'] = $nome;
$arrFolha1[$matr]['cd_lot'] = $cd_lot;
$arrFolha1[$matr]['nm_lot'] = $nm_lot;
}
}
Ex2:
while ($linha = mysql_fetch_array($rs)) {
$matr = $linha['matr'];
$cd_lot = trim($linha['cd_lot']);
$ap_freq = $linha['ap_freq'];
if ($matr_ant != $matr){
$aAtuacao[$matr] = array();
$i = 0;
$matr_ant = $matr;
}
$aAtuacao[$matr][$i] = array();
$aAtuacao[$matr][$i]['cd_lot']=$cd_lot;
$aAtuacao[$matr][$i]['ap_freq']=$ap_freq;
$i++;
}
I would like some tips on how and when to use auto increment $i++ and $i=0, inside and outside of the loop for and while. thank you