Jump to content

[SOLVED] for loop seems to be skipping some numbers or some thing else very sinister


riceje7

Recommended Posts

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);
?>

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;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.