PHP警告:in_array():第二个参数的数据类型错误


PHP Warning: in_array() : Wrong datatype for second argument

注意:我意识到这是旧代码,但我希望修复它,直到明年我们可以转移到新系统。

我从下面的两行代码中得到了这两个重复出现的错误。

如何修复这些错误?

错误:

PHP警告:in_array()[function.in-array]:第二个参数行120 的数据类型错误

PHP致命错误:在非对象行121 上调用成员函数add_viewed()

导致错误的代码行:

if (!in_array($HTTP_GET_VARS['products_id'], $items_ids_on_display)) {
  $viewed->add_viewed($HTTP_GET_VARS['products_id']);
}

下方文件的完整代码

if (((tep_session_is_registered('customer_id')) or (ENABLE_PAGE_CACHE == 'false')) and (!$spider_flag)){

//*******************************************************************************
  DEFINE('HIST_ROWS', 7);         // number of rows per column on display
  DEFINE('HIST_MAX_ROWS',7);     // max number of products on display
  DEFINE('HIST_MEM_TRIGGER', 1);  // number when memory threshold kicks in
//*******************************************************************************
  // register the array if not already done so
  if (tep_session_is_registered('viewed') && is_object($viewed)) {
  } else {
    tep_session_register('viewed');
    $viewed = new viewed_products;
    $viewed->reset();
  }
  // empty the array if requested by the user
      if (isset($HTTP_GET_VARS['action'])) {
        if ($HTTP_GET_VARS['action'] == 'viewed_remove') {
          $viewed->remove();
        }
     }

// start shift from line 106 to here
 $items_ids_on_display = array();
// end shift

  // display the box if we have history
  if ($viewed->count_viewed() > 0) { // displaying
  ?>
    <tr>
  <td class="prodRowDivide">
  <table border="0" width="100%" cellpadding="2" cellspacing="1">
  <tr class="header">
    <td>
  <table border="0" width="100%" cellspacing="0" cellpadding="0">
  <?php
      echo '<tr><td nowrap valign="center" class="prodRowHead" height="22">Recently Viewed Products:<br></td></tr>
      </table>
      </td></tr>
      <tr><td valign="top"><table border="0" cellpadding="3" align="left"><tr>'; 
    $info_box_contents = array();
    //$info_box_contents[] = array('text' => 'Recently Viewed');
    //new infoBoxHeading($info_box_contents, false, false);
    $row = 0;
    $col = 0;
    /* get the products array from the class containing all viewed products */

    $items = $viewed->get_viewed_items();

    $index = 1;
    /* determine the first and last record we want to display*/
    $first = sizeof($items)- HIST_MAX_ROWS;
    $last  = sizeof($items)-1;
    if (($last+1) < HIST_MAX_ROWS) {$disp = ($last+1);} else {$disp = HIST_MAX_ROWS;}
    if ($first < 0) {$first = 0;}
    /* only fetch the info for products on display */
//    $items_ids_on_display = array();          // shift to line 67
    for ($i=$last, $n=$first; $i>=$n; $i--) {
        $viewed_query = tep_db_query("select pd.products_name,
                                             p.products_image_lrg
                                      from " . TABLE_PRODUCTS . " p,
                                           " . TABLE_PRODUCTS_DESCRIPTION . " pd
                                      where p.products_id = '" . $items[$i] . "' and
                                            pd.language_id = '" . $languages_id . "' and
                                            pd.products_id = p.products_id");
        if ($viewed_info = tep_db_fetch_array($viewed_query)) {
         $items_on_display[$i] = array('id' => $items[$i],
                                     'name' => $viewed_info['products_name'],
                                     'image' => $viewed_info['products_image_lrg']);
         $items_ids_on_display[]= $items[$i];
        }
    }
    for ($i=$last, $n=$first; $i>=$n; $i--) {
        $currentPage = (int)($HTTP_GET_VARS['products_id']);
    if ($currentPage != $items[$i]) {
    echo '<td align="left" class="smallText"><a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $items_on_display[$i]['id']) . '">'. tep_image_thumb(DIR_WS_IMAGES . $items_on_display[$i]['image'], $items_on_display[$i]['name'], '120', '120') . '</a></center></td>';
      $row ++;
      $index++;
      }
    }
    ?>
</tr></table></td></tr>
  </table>
  </td>
  </tr>
   <tr>
      <td valign="top"><?php echo tep_draw_separator('pixel_trans.gif', '100%', '5'); ?></td>
   </tr>
<?php
  }
 // general condition
}
  if (isset($HTTP_GET_VARS['products_id']) and ($HTTP_GET_VARS['action'] != 'viewed_remove')) {
    if (!in_array($HTTP_GET_VARS['products_id'], $items_ids_on_display)) {
      $viewed->add_viewed($HTTP_GET_VARS['products_id']);
    }
} ?>

提前感谢您的帮助!

对于第一个错误:

PHP警告:in_array()[function.in-array]:第二个参数行120 的数据类型错误

您正在将而非数组的值传递到in_array中。您可以从文档中看到,第二个参数明确需要一个数组。这是因为,如果代码顶部的第一个if语句无效,则$items_ids_on_display是未设置的。

"如何修复此错误?"

在尝试在in_array中使用$item_ids_on_display之前,请检查其是否存在。

线路

$viewed->add_viewed($HTTP_GET_VARS['products_id']);

据报道,正试图使用add_viewed()的方法将$viewed的值用作对象,其中$viewed的值实际上不是对象(当不满足适用的if语句时,它是未设置的。)

"如何修复此错误?"

和以前一样,在尝试将$viewed用作对象之前,请检查其是否存在。

忽略任何其他代码审查并专注于报告的问题,您在底部得到的代码应该是:

if (isset($items_ids_on_display) && isset($viewed) && isset($HTTP_GET_VARS['products_id']) and ($HTTP_GET_VARS['action'] != 'viewed_remove')) {
    if (!in_array($HTTP_GET_VARS['products_id'], $items_ids_on_display)) {
      $viewed->add_viewed($HTTP_GET_VARS['products_id']);
    }
}

最后注释

您应该使用error_reporting(E_ALL)集进行编程,这样您就可以看到PHP通知以及PHP警告和错误。它们将让您更深入地了解为什么会出现警告和错误。