Solved

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

Posted on 2006-11-19
1
186 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
This article discusses four methods for overlaying images in a container on a web page
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now