【问题标题】:Comparing Arrays and Replacing Values Where Necessary in a Function比较数组和替换函数中必要的值
【发布时间】:2014-04-09 22:59:03
【问题描述】:

谁能帮我解释一下为什么这个功能不起作用?

我实际上是在寻找数组 $bookings 并为每一行检查 $exceptions 中是否存在相应的行(基于日期和 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”的字段,它返回数据。

任何帮助将不胜感激!

谢谢!

【问题讨论】:

  • 请指出第264行。
  • 你的 foreach 循环没有意义。您将立即重新分配迭代变量。

标签: php arrays function mysqli


【解决方案1】:

我认为这行:

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

应该是:

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

【讨论】:

  • 你是对的,正如大多数人认为它在被指出时看起来很简单。非常感谢您的帮助,不胜感激!
【解决方案2】:

在您的 mergeMyBookings 功能上。 $bookings 数组必须有如下键:

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

【讨论】:

    【解决方案3】:

    或者你需要像这样的另一个 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'], 
                    );
            }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多