1

I have a function to loop through multidimensional array :

function in_multiarray($elem, $array, $field)
{
    $top = sizeof($array) - 1;
    $bottom = 0;
    while($bottom <= $top)
    {
      if($array[$bottom][$field] == $elem)
        return true;
      else 
        if(is_array($array[$bottom][$field]))
            if(in_multiarray($elem, ($array[$bottom][$field])))
                return true;
      $bottom++;
    }        
   return false;

}

Then, I use it like this :

$hoursoff = array( array("10:00" => "2016-10-07", "11:00" => "2016-10-07", "12:00" => "2016-10-07"), array("10:00" => "2016-10-08", "11:00" => "2016-10-08") );

if( in_multiarray("$date", $hoursoff, "$hour") ) { /* do it */ } else { /* don't do it */ }
 /* $date and $hour come from database request */

this works fine. But when I check my error_reporting(E_ALL); it throws

 Notice: Undefined index ...

I know it's no big deal, and does not affect results, but in order to learn from this : * which part of the script is involved in this error ? * how do I avoid having this (or what am I doing wrong) ? thanks

WitVault
  • 23,445
  • 19
  • 103
  • 133
OldPadawan
  • 1,247
  • 3
  • 16
  • 25

3 Answers3

0

Use for loop instead of while loop as you could work with index of your array first

Then you're checking if $date exist there is no key in your array and key starting with $ is not a right index ; in your 2nd dimension index are "10:00" , "11:00" and not date or hour

MounirOnGithub
  • 669
  • 1
  • 9
  • 22
0

I would also test for isset($array[$bottom]) and isset($array[$bottom][$field]) in all if statements.

0

If your multidimensionnal array doesn't have the same structure for every elements you might want to add some isset() functions to test if the array with its associated key exists before you do a test on it inside an if() statement.

Alex
  • 478
  • 2
  • 11