riceje7 Posted November 19, 2009 Share Posted November 19, 2009 for some reason when i run the script the for loop in the countwords function seems to be skipping some numbers causing dozens of Notice: Undefined offset errors. i've been staring at this for hours and can't figure it out. can anyone see something that i'm doing wrong? thanks main code: <?php include 'db_connect.php'; include 'parsetext.php'; include 'initswords.php'; include 'countwords.php'; if(array_key_exists('submit2', $_POST)){ $temptext = $_POST['text']; $textinput = parsetext($temptext); } if(array_key_exists('submit', $_POST)){ $temp = $_FILES['textfile']; //var_dump($temp); if($temp['type'] == "text/plain" || $temp['type'] == "application/rtf" || $temp['type'] == "application/pdf"){ $tempfile = $_FILES['textfile']['name']; $temptype = $_FILES['textfile']['type']; $uploaddir = 'textfile_backups/'; $uploadfile = $uploaddir . basename($_FILES['textfile']['name']); if (move_uploaded_file($_FILES['textfile']['tmp_name'], $uploadfile)) { //echo "Success"; } } else{ //echo "Failure"; } $fp = fopen($uploadfile, 'r'); $tempfile = fread($fp, filesize($uploadfile)); fclose($fp); $filearray = parsetext($tempfile); $returncount = countwords($filearray); $uniquearray = array_unique($filearray); //print_r($uniquearray); for($i = 0; $i < count($filearray); $i++){ echo $uniquearray[$i]."=>".$returncount[$i]."<br/>"; } } ?> parsetext.php: <?php function parsetext($text){ rtrim($text); $text = strtolower($text); $text = preg_replace("/[^a-zA-Z0-9 ]/",'',$text); $textarray = explode(" ", $text); return $textarray; } ?> countwords.php: <?php function countwords($temp){ $tempcount = array_count_values($temp); $temp2 = array_unique($temp); print_r($temp2); $temparray = array(); //echo(count($temp)); for($i = 0; $i < count($temp); $i++){; $temparray[$i] = $tempcount[$temp2[$i]]; } return $temparray; } ?> initswords.php: <?php $tempfilename = "stopwords.txt"; $fp = fopen($tempfilename, "r"); $tempDATA = fread($fp, filesize($tempfilename)); fclose($fp); ?> Quote Link to comment https://forums.phpfreaks.com/topic/182199-solved-for-loop-seems-to-be-skipping-some-numbers-or-some-thing-else-very-sinister/ Share on other sites More sharing options...
riceje7 Posted November 19, 2009 Author Share Posted November 19, 2009 added code that i forgot before. intswords.php is now added. any help would be absolutely amazing Quote Link to comment https://forums.phpfreaks.com/topic/182199-solved-for-loop-seems-to-be-skipping-some-numbers-or-some-thing-else-very-sinister/#findComment-961370 Share on other sites More sharing options...
Ken2k7 Posted November 19, 2009 Share Posted November 19, 2009 What does the function countwords supposed to do? Count the number of words or the number of unique words? Quote Link to comment https://forums.phpfreaks.com/topic/182199-solved-for-loop-seems-to-be-skipping-some-numbers-or-some-thing-else-very-sinister/#findComment-961375 Share on other sites More sharing options...
Mchl Posted November 19, 2009 Share Posted November 19, 2009 Are you sure $temp2 has at least as many elements as $temp ? Quote Link to comment https://forums.phpfreaks.com/topic/182199-solved-for-loop-seems-to-be-skipping-some-numbers-or-some-thing-else-very-sinister/#findComment-961379 Share on other sites More sharing options...
DavidAM Posted November 19, 2009 Share Posted November 19, 2009 from the manual http://us.php.net/manual/en/function.array-unique.php array_unique() Takes an input array and returns a new array without duplicate values. Note that keys are preserved. array_unique() sorts the values treated as string at first, then will keep the first key encountered for every value, and ignore all following keys. It does not mean that the key of the first related value from the unsorted array will be kept. (emphasis added) So the keys are not going to be consecutive. Which means you can't walk the resulting array with an incrementing index. What are you expecting from countwords()? The way it is written you should get a numerically indexed array with a count assigned to each value, but you have no way of knowing which word that count applies to. I think $tempcount has what you really want; an array indexed by words with a count of the number of times that word is found in the original array. You can print it using foreach: foreach ($tempcount as $word => $count) { print $word . ' occurs ' . $count . ' time(s)<BR>' .PHP_EOL; } Quote Link to comment https://forums.phpfreaks.com/topic/182199-solved-for-loop-seems-to-be-skipping-some-numbers-or-some-thing-else-very-sinister/#findComment-961400 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.