Friday, August 3, 2018

Problem solved for undefined offset notice when looping through arrays in php

Hello friends,

Today I am sharing very important code which is people searching online but solution is not available and that was asked in the following url of stackoverflow.com

https://stackoverflow.com/questions/3234616/undefined-offset-notice-when-looping-through-arrays-in-php/51672458#51672458

Please go with the below code, hope that will help you in your projects.

  <select title="mm" name="month" id="month" class="">
  <?php
  error_reporting(E_ALL & ~E_NOTICE);
  $datearray=array('Jan','Feb','Mar');
  for($d=0;$d<=sizeof($datearray);$d++){ ?>
  <?php if($datearray[$d]=='' OR $datearray[$d]==null) continue; ?>
           <option><?php echo $datearray[$d]; ?></option>
    <?php } ?>

  </select>


In the above code I have just updated two statement which is present in bold text.

- Error_reporting with (E_ALL & ~E_NOTICE) is important for removing notice error from whole page.
- Continue statement with code if($datearray[$d]=='' OR $datearray[$d]==null) continue; under for loop saying that if you will get blank or null value please continue whole loop without breaking anything.


Have any query in the above solutions, please put your comment in the below section.

No comments:

Post a Comment