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 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程