dougutuo9879 2015-01-21 19:53
浏览 56
已采纳

警告:非法偏移类型错误

I'm getting "illegal offset type" error for line $wordCountArr[$val]['bytotal'] = $wordCountArr[$val]['count'] / $totalWords; of this code. Here's the code in case anyone can help:

<?php

function extractCommonWords($string)
{
  $stopWords = array('i','a','about','an','and','are','as','at','be','by','com','de','en','for','from','how','in','is','it','la','of','on','or','that','the','this','to','was','what','when','where','who','will','with','und','the','www');

  $string = preg_replace('/ss+/i', '', $string);
  $string = trim($string); // trim the string
  $string = preg_replace('/[^a-zA-Z0-9 -]/', '', $string); // only take alphanumerical characters, but keep the spaces and dashes too…
  $string = strtolower($string); // make it lowercase

  preg_match_all('/\b.*?\b/i', $string, $matchWords);
  $matchWords = $matchWords[0];

  $totalWords = count($matchWords[0]);

  foreach ( $matchWords as $key=>$item ) {
      if ( $item == '' || in_array(strtolower($item), $stopWords) || strlen($item) <= 3 ) {
          unset($matchWords[$key]);
      }
  }
  $wordCountArr = array();
  if ( is_array($matchWords) ) {
      foreach ( $matchWords as $key => $val ) {
          $val = strtolower($val);
          if ( !isset($wordCountArr[$val])) {
              $wordCountArr[$val] = array();
          }
          if ( isset($wordCountArr[$val]['count']) ) {
              $wordCountArr[$val]['count']++;
          } else {
              $wordCountArr[$val]['count'] = 1;
          }
      }
      arsort($wordCountArr);
      $wordCountArr = array_slice($wordCountArr, 0, 10);
      foreach ( $wordCountArr as $key => $val) {
          $wordCountArr[$val]['bytotal'] = $wordCountArr[$val]['count'] / $totalWords;
      } 
  }
  return $wordCountArr;
}

$text = "AES algo to encrypt files.";
$words = extractCommonWords($text);
echo implode(',', array_keys($words));
?>
  • 写回答

2条回答 默认 最新

  • dongyuyi5680 2015-01-21 20:00
    关注

    Look your entire foreach loop:

    Change the variable $wordCountArr to $val:

    foreach ( $wordCountArr as $key => $val) {
          $val['bytotal'] = $val['count'] / $totalWords;
      }
    

    Hope it helps you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题