I am trying to remove duplicates from a set of tokens using unset (not considering array_unique for now), however I am getting a few issues.
$keywords = parseTweet ( $tweet );
$term_freq = array(count($keywords));
for($i = 0; $i < count($keywords); $i++){
$term_freq[$i] = 1;
for($j = 0; $j < count($keywords); $j++){
if (($i != $j) && (strcmp($keywords[$i],$keywords[$j]) == 0)){
unset ( $keywords [$j] );
unset ( $term_freq [$j] );
$term_freq[$i]++;
}
}
}
print_r ( $keywords );
print_r ( $term_freq );
I am aware of why I am getting an error; while the duplicate $j is removed, the for loop still has to reloop for the rest of the keywords and hence will fail when it encounters the missing $j. Checking the contents of the array, I found out that the index of the array skips the index $j. So it reads; [1], [2], [4], ... etc where $j = [3]
I thought that unset also rebalances the array index, am I doing something wrong or missing something completely? I am new to PHP so please bear with me!
unset ( $term_freq [$j] ); $term_freq[$j]++;you unset it and want to increase it?$term_freqwithcount($keywords)? the first iteration of theforloop is going to overwrite this.foreachinstead offor, it will just see the elements that still exist.