Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Logic error while iterating through for loop


Reply


3 Posts   407 Views

Avatar
Epro95Z28

Community Member, 4 Posts

25 April 2012 at 2:33am

Hey all I'm getting a "[Notice] Undefined offset: 0" silverstripe error message while trying to go through a for loop

for ($i = 0; $i < count($aStartDate); ++$i) {
                     $duplicateCount = 0;
                     //print $aStartDate[$i];
                        for ($g = 0; $g < count($aStartDate); ++$g) {
                           if ($aStartDate[$i] == $aStartDate[$g]) {
                              $duplicateCount++;                           
                        }
                        
                     }
                     if ($duplicateCount != 0) {
                           $dateToReplace = $aStartDate[$i];
                           unset($aStartDate[$i]);
                           unset($aStartDate[$g]);
                        }
                  }

The error message shows up on the "if ($aStartDate[$i] == $aStartDate[$g]) {" line.

Help is much appreciated :)

Avatar
Epro95Z28

Community Member, 4 Posts

25 April 2012 at 2:53am

What I'm trying to do is go through an array and remove duplicate entries and replace them the amount of entries as a single entry.

The end result is that when there's more than one event happening, it will says "two events". After you click on it, it loads the data for that date (It does an AJAX request based on the StartDate and displays all events)

Confusing? Yeah maybe, losing almost all my hair with this calendar module... blegh

Avatar
Willr

Forum Moderator, 5513 Posts

25 April 2012 at 4:15pm

Perhaps array_count_values() would be easier? http://php.net/manual/en/function.array-count-values.php