Notice: Undefined offset looking inside array with for

2

Hello

My code is a calendar and it works as follows, there is an array with some times inside:

$horarios = ["08:00", "09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00"];

I then do a SELECT on the database looking for schedules and store the data inside an array through a foreach:

$stmt = $dbh->query("SELECT *, date_format(hora_agendamento, '%H%:%i') as hora_agendamento FROM agendamentos WHERE data_agendamento='$data_agendamento_convert' AND medico='$medico_completo'");
  $result = $stmt->fetchAll();

After this through a for I walk inside the array and check if each time of the $ schedule array can be found inside the $ result array:

for ($i=0; $i <= (count($horarios) - 1); $i++) {
  if (in_array($horarios[$i], $result[$i])) { ?>
    <tr>
      <td><?php echo $horarios[$i]; ?></td>
      <td><?php echo $result[$i]['nome']; ?></td>
      <td><?php echo $result[$i]['descricao']; ?></td>
      <td>Editar</td>
    </tr>
 <?php } else { ?>
    <tr>
       <td><?php echo $horarios[$i]; ?></td>
       <td></td>
       <td></td>
       <td>Editar</td>
     </tr>
 <?php } }?>

The first line found is printed correctly, then I believe that because I have 10 times in $ time and only 3 matches in $ result the code starts to return the error below:

  

Notice: Undefined offset: 3 in C: \ wamp64 \ www \ admin \ agenda1.php on line 91

I imagine if I improve this if I can solve the problem, but I do not know what to use to improve it, can someone give me a light?

Thank you!

    
asked by anonymous 15.05.2018 / 16:09

1 answer

2

Do this:

if (isset($result[$i]) && in_array($result[$i], $horarios)) { ?>

So this error will stop appearing because isset($result[$i]) checks if the variable with the index inside array $result EXISTS. It will return true if it exists and as long as this value is not null.

$array = array(0 => "oi", 1 => "", 2 => null, 3 => false);

isset( $array [0] ) // true
isset( $array [1] ) // true
isset( $array [2] ) // false
isset( $array [3] ) // true

In addition, as quoted by Virgilio Novic in the comments, the truth is:

in_array ("valor que eu procuro", array());

View the documentation

So you need to put $result[$i] before $horarios . Since $horarios is already an array (), you do not need to indicate an index in $horarios .

EDIT

Depending on the image shown, you can loop within your loop to analyze each case. See:

for($i = 0; $i < count($horarios); $i++){
    $status = true;
    foreach($result as $resultado){
        if($horarios[$i] == $resultado['hora_agendamento']){
        ?>
            <tr>
              <td><?php echo $horarios[$i]; ?></td>
              <td><?php echo $resultado['nome']; ?></td>
              <td><?php echo $resultado['descricao']; ?></td>
              <td>Editar</td>
            </tr>
        <?php 
            $status = false;
            break;
        }
    }
    if($status){ ?>

    <tr>
       <td><?php echo $horarios[$i]; ?></td>
       <td></td>
       <td></td>
       <td>Editar</td>
     </tr>

<?php }}?>

EDIT 2

In spite of the other answer, this example below, with array_search is more optimized.

$horariosCheios = array();
foreach($result as $resultado){
    $horariosCheios[] = $resultado['hora_agendamento'];
}

for ($i=0; $i <= (count($horarios) - 1); $i++) {
  if (($indice = array_search($horarios[$i], $horariosCheios)) !== false) { ?>
    <tr>
      <td><?php echo $horarios[$i]; ?></td>
      <td><?php echo $result[$indice]['nome']; ?></td>
      <td><?php echo $result[$indice]['descricao']; ?></td>
      <td>Editar</td>
    </tr>
 <?php } else { ?>
    <tr>
       <td><?php echo $horarios[$i]; ?></td>
       <td></td>
       <td></td>
       <td>Editar</td>
     </tr>
 <?php } }?>
    
15.05.2018 / 19:16