jsims281forum Posted December 14, 2006 Share Posted December 14, 2006 What i need to do: collect two pieces of text from an html form and produce a list of the words they have in common, and how many times each common word appears in each text.E.G.Word |Times in Tx1 | Times in Tx2Hello |5 | 3At the moment I've got three main arrays, one containing each of the texts and one containing the shared words. I've got a big chunk of code but the results it produces are...well, let's say they have erroneous tendancies. Heres some of my code so you can see what I'm trying to do (hopefully it's clear enough to not need too much background, or a full listing). This is the first thing I've ever done in php, and the first thing I've had to program in any language in a long time, so you'll have to let me know if I'm making any fundamental or stupid mistakes! [code]//generating the array of shared words$shared = array();$kk=0;for ($ii =0; $ii<$maxwordcount;$ii++){ for ($jj =0; $jj<$maxwordcount;$jj++) { if ($arrayofwords[$ii] == $arrayofwords2[$jj]) { if (in_array($arrayofwords[$ii],$shared)) { //$counter[$kk-1]++; } else { $temp1 = $arrayofwords[$ii]; $shared[$kk]=$temp1; $kk++; } } }}[/code]And here, I'm trying to count how many times each shared word appears in text1 (but it doesn't really work):[code]$count1 = array();$words1 = array();for ($ab=0;$ab<count($shared);$ab++){if (in_array($shared[$ab],$arrayofwords)) { if (in_array($shared[$ab],$shared)) //if it's already in the shared array... { for ($xy=0;$xy<count(shared)+1;$xy++) { if ($shared[$ab]==$shared[$xy]) //find it and increment the counter { $count1[$ab] = ($count1[$ab] +1); $words1[$ab]=$shared[$ab]; } } } else{ $count1[$ab] = ($count1[$ab] +1); $words1[$ab]=$shared[$ab];} }}[/code]Any suggestions on an easier way to achieve this, or help on where I'm going logically wrong would be much appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/30664-solved-need-help-with-arrays-counting-logic/ Share on other sites More sharing options...
sasa Posted December 14, 2006 Share Posted December 14, 2006 try[code]<?php$w[1]="one two none one one xx prpr one none";$w[2]="one two one none none xxxx one none none";$word_array[1] = explode(' ',$w[1]);$word_array[2] = explode(' ',$w[2]);$shared = array_intersect($word_array[1], $word_array[2]);for ($i=1; $i<3; $i++) { foreach ($word_array[$i] as $a) { if (in_array($a, $shared)) $out[$a][$i]++; }}echo '<table border="2"> <tr> <td>Word</td> <td>count in Tx1</td> <td>count in Tx2</td> </tr>';foreach ($out as $w => $b) echo " <tr> <td>$w</td> <td>$b[1]</td> <td>$b[2]</td> </tr>";echo '</table>';?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/30664-solved-need-help-with-arrays-counting-logic/#findComment-141348 Share on other sites More sharing options...
jsims281forum Posted December 14, 2006 Author Share Posted December 14, 2006 That's fantastic, it works like a charm as far as I can see so far :D Cheers sasa, great help! Quote Link to comment https://forums.phpfreaks.com/topic/30664-solved-need-help-with-arrays-counting-logic/#findComment-141361 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.