Solved

Notice: Undefined index (how do I fix this?)

Posted on 2006-11-19
1
189 Views
Last Modified: 2008-02-20
Hello,

This script works but gives me a load of notices:
         Undefined index

I am running PHP Version 5.1.5.


Please help!
Thanks.

<?php
$pagecontents = null;
$keys = null;
$freqall = null;
$freqarray = null;
$freqarray2 = null;
$freqarray3 = null;

$commonwords = 'a an about';

$sitecontents = "This is a test and only a test.";

$parsearray[] = $sitecontents;
$parsestring = strtolower(join($parsearray," "));
$parsestring = str_replace (",", "", $parsestring);
$parsestring = str_replace ("\n", "", $parsestring);
$parsestring = str_replace (")", "", $parsestring);
$parsestring = str_replace ("(", "", $parsestring);
$parsestring = str_replace (".", "", $parsestring);
$parsestring = str_replace ("'", "", $parsestring);
$parsestring = str_replace ('"', "", $parsestring);

$commonarray = split(" ",$commonwords);

for ($i=0; $i<count($commonarray); $i++) {
   $parsestring = str_replace (" ".$commonarray[$i]." ", " ", $parsestring);
}

$parsestring = str_replace ("  ", " ", $parsestring);
$parsestring = str_replace ("  ", " ", $parsestring);
$parsestring = str_replace ("  ", " ", $parsestring);

$wordsarray = split(" ",$parsestring);

for ($i=0; $i<count($wordsarray); $i++) {
   $word = $wordsarray[$i];
   if ($freqarray[$word]) {
       $freqarray[$word] += 1;
   } else {
       $freqarray[$word]=1;
   }
}

arsort($freqarray);

$i=0;
while (list($key, $val) = each($freqarray)) {    
   $i++;
   $freqall[$key] = $val;
   if ($i==15) {
      break;
   }
}

for ($i=0; $i<count($wordsarray)-1; $i++) {
   $j = $i+1;
   $word2 = $wordsarray[$i]." ".$wordsarray[$j];
   if ($freqarray2[$word2]) {
       $freqarray2[$word2] += 1;
   } else {
       $freqarray2[$word2]=1;
   }
}

arsort($freqarray2);

$i=0;
while (list($key, $val) = each($freqarray2)) {    
   $i++;
   $freqall[$key] = $val;
   if ($i==4) {
      break;
   }
}

for ($i=0; $i<count($wordsarray)-2; $i++) {
   $j = $i+1;
   $word3 = $wordsarray[$i]." ".$wordsarray[$j]." ".$wordsarray[$j+1];
   if ($freqarray3[$word3]) {
       $freqarray3[$word3] += 1;
   } else {
       $freqarray3[$word3]=1;
   }
}

arsort($freqarray3);

$i=0;
while (list($key, $val) = each($freqarray3)) {    
   $i++;
   $freqall[$key] = $val;
   if ($i==1) {
      break;
   }
}

arsort($freqall);

while (list($key, $val) = each($freqall)) {    
   $pagecontents .= "$key => $val<br>";
   $keys .= $key.", ";
}

chop($keys);

$pagecontents .= $keys;

echo $pagecontents;

?>
0
Comment
Question by:hankknight
1 Comment
 
LVL 29

Accepted Solution

by:
TeRReF earned 500 total points
ID: 17974906
Use array_key_exists() to check if keys exist in an array
http://php.net/array_key_exists
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question