Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

illegal offset type in php on vscode no error, but on page says that error

hello i’m running into a problem, it says on the browser illegal offset type,
im declaring an array this way:

 $matriculas = [
        1 => ["99-99-99", "D"],
        2 => ["88-88-88", "D"],
    ];

and the error is in this line :

 $series[$option] = [$option['matricula'],$option['type']];

the function looks like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<?php 
    $query ="SELECT * FROM matriculas";
    $result = $con->query($query);
    if($result->num_rows> 0){
      $options= mysqli_fetch_all($result, MYSQLI_ASSOC);
    }
    $series = array();
    foreach ($options as $option) {
        $series[$option] = [$option['matricula'],$option['type']];
    }
?>
<select name="id">
   <option>Select matricula</option>
<?php 
  foreach ($series as $ID => $values) {
?>
    <option name=<?php $ID ?> > <?php echo $values[0]; ?></option>
<?php 
}
>
</select>

how can i make it right? thanks in advance for your help

>Solution :

It’s almost right.

Based on the subsequent foreach ($series as $ID => $values), I think you want this instead:

foreach ($options as $option) {
    $series[$option['id']] = [$option['matricula'], $option['type']];
}

But unless you’re going to use the $option['type'] value for something else later, it could be simplified to

foreach ($options as $option) {
    $series[$option['id']] = $option['matricula'];
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading