0

I have defined a const with an array.

CONST DEFAULT_PRICES = array(30, 20, 10);

Now I have used this constant inside in function like below:

foreach ($workingDays as $selectedDate) {
    //get the index 
    $index = array_search($selectedDate, $workingDays);

    $price = $basePrice + ($basePrice * self::DEFAULT_PRICES[$index ] / 100);
    $price = sprintf("%.2f", $price);

    $dateArr[$selectedDate] = $basePrice;

}

But I am getting error saying Message: Undefined offset: 3

can anybody tell me why?

S S
  • 1,443
  • 2
  • 12
  • 42
  • 1
    array indexes start at 0, so your constant has indexes 0, 1 and 2; not 3. Perhaps you need to subtract 1 from `$index`? – Nick Apr 29 '19 at 08:29
  • @Nick then i get Message: Undefined offset: -1 – S S Apr 29 '19 at 10:13
  • Sounds like maybe you need more elements in your `DEFAULT_PRICES` array then? You need to check what the actual values of `$index` being produced are and adjust your array to suit. – Nick Apr 29 '19 at 23:21

0 Answers0