比较数组和替换函数中必要的值

时间:2014-03-07 00:38:51

标签: php arrays function mysqli

任何人都可以帮我解释为什么这个功能无效?

我基本上想要获取数组$ bookings并且每行检查相应的行是否存在于$ exceptions(基于date和user_id)中,如果是这样,则使用$ exceptions中的值覆盖$ bookings中的内容。如果$ exceptions中没有匹配的行,那么继续。

这是数组结构:

   // Get Bookings  
   $query = "SELECT bookings.id, bookings.user_id, bookings.class_id, bookings.class_date, bookings.booking_status, 
   classes.class_name, classes.start_time, classes.end_time, studios.studio_name, staff.firstname, staff.surname 
   FROM bookings 
   LEFT JOIN classes ON bookings.class_id=classes.classes_id 
   LEFT JOIN studios ON classes.studio_id=studios.id 
   LEFT JOIN staff ON classes.instructor_id=staff.id
   WHERE bookings.user_id='$id' AND bookings.class_date BETWEEN '2014-02-28' AND '2014-03-15'";
   $result = mysqli_query($sql, $query);

   while($row = mysqli_fetch_assoc($result)) {
   $bookings[$row['id']] = array('user_id' => $row['user_id'], 'class_id' => $row['class_id'], 'class_date' => $row['class_date'], 'booking_status' =>   $row['booking_status'], 'class_name' => $row['class_name'], 'start_time' => $row['start_time'], 'end_time' => $row['end_time'], 'studio_name' =>      $row['studio_name'], 'firstname' => $row['firstname'], 'surname' => $row['surname']);
   }
   mysqli_free_result($result);


   // Get Exceptions
   $query = "SELECT class_exceptions.id, class_exceptions.class_id, class_exceptions.class_date, class_exceptions.exc_name,     
   class_exceptions.exc_starttime, class_exceptions.exc_endtime, studios.studio_name, staff.firstname, staff.surname 
   FROM class_exceptions 
   LEFT JOIN studios ON class_exceptions.exc_studio=studios.id
   LEFT JOIN staff ON class_exceptions.exc_instructor=staff.id
   WHERE class_exceptions.class_date BETWEEN '2014-02-28' AND '2014-03-15'";
   $result = mysqli_query($sql, $query);

   while($row = mysqli_fetch_assoc($result)) {
   $exceptions[$row['id']] = array('class_id' => $row['class_id'], 'class_date' => $row['class_date'], 'exc_name' => $row['exc_name'], 
   'exc_starttime' => $row['exc_starttime'], 'exc_endtime' => $row['exc_endtime'], 'studio_name' =>   $row['studio_name'], 
   'firstname' => $row['firstname'], 'surname' => $row['surname']);
   }
   mysqli_free_result($result);



createMyBookings($bookings, $exceptions);

然后功能如下:

function createMyBookings ($bookings, $exceptions) {

   foreach($bookings as $details) {                   

      $details = mergeMyBookings($bookings, $exceptions);

               echo $details['class_name'];
               echo '<br>';
               echo $details['firstname'] . ' ' . $details['surname'];
               echo '<br>';
               echo $details['studio_name'];
               echo '<br>';
               echo date('H:i',strtotime($details['start_time'])) . ' - ' . date('H:i',strtotime($details['end_time']));                
               echo '<br>';
   }
}




function mergeMyBookings($bookings, $exceptions){
    foreach($exceptions as $exception){
        if(($exception['class_id'] == $bookings['class_id']) && ($exception['class_date'] == $bookings['class_date']))
            return array(
                'id' => $bookings['id'],
                'user_id' => $bookings['user_id'],
                'class_id' => $bookings['class_id'],
                'class_date' => $bookings['class_date'],
                'booking_status' => $bookings['booking_status'],    
                'class_name' => $exception['exc_name'],
                'start_time' => $exception['exc_starttime'],
                'end_time' => $exception['exc_endtime'],
                'studio_name' => $exception['studio_name'],
                'firstname' => $exception['firstname'],
                'surname' => $exception['surname'], 
            );
    }
    return $details;
}

我马上得到的错误是:

Notice: Undefined index: class_id in C:\xampp\htdocs\includes\functions.php on line 264

这很奇怪,因为我已经对数组进行了三次检查,当我打印它们时,它们都有一个名为“class_id”的字段,它返回数据。

非常感谢任何帮助!

谢谢!

3 个答案:

答案 0 :(得分:2)

我认为这一行:

$details = mergeMyBookings($bookings, $exceptions);

应该是:

$details = mergeMyBookings($details, $exceptions);

答案 1 :(得分:0)

在mergeMyBookings函数上。 $ bookings数组必须有一个键,如:

if(($exception['class_id'] == $bookings[$key_value]['class_id']) && ($exception['class_date'] == $bookings[$key_value]['class_date']))

答案 2 :(得分:0)

或者你需要另一个这样的foreach:

foreach($exceptions as $exception){
    foreach($bookings as $booking){
            if(($exception['class_id'] == $booking['class_id']) && ($exception['class_date'] == $booking['class_date']))
                return array(
                    'id' => $booking['id'],
                    'user_id' => $booking['user_id'],
                    'class_id' => $booking['class_id'],
                    'class_date' => $booking['class_date'],
                    'booking_status' => $booking['booking_status'],    
                    'class_name' => $exception['exc_name'],
                    'start_time' => $exception['exc_starttime'],
                    'end_time' => $exception['exc_endtime'],
                    'studio_name' => $exception['studio_name'],
                    'firstname' => $exception['firstname'],
                    'surname' => $exception['surname'], 
                );
        }
}