I have a variable which holds some words: $words = 'toyota, bmw, audi, opel, audi, bmw, renault'; How do I get to this result of uniwue words holding the number of occurences, ordered by occurences descending: bmw(2) audi(2) toyota(1) opel(1) renault(1) Thank you for your help.
<?php $words = 'toyota, bmw, audi, opel, audi, bmw, renault'; $tmp = array_map('trim', explode(',', $words)); sort($tmp); foreach (array_count_values($tmp) AS $word => $count) { echo "{$word} ({$count})<br />\n"; } ?> PHP:
wow..that did the trick. Is there any way of ordering them by occurences descending? Thank you very much.
beat me too it <?php $words = 'toyota, bmw, audi, opel, audi, bmw, renault, bmw, audi, audi, audi,audi'; function count_list_to_array( $words ) { $list = array_count_values( split(", ?", $words ) ) ; return $list; } $list = count_list_to_array( $words ) ; arsort( $list ); foreach( $list as $index => $occured ) { printf("%s ( %s )<br/>\n", $index, $occured ); } PHP:
Ok. Thanks a lot guys. Here's my application so far which works very well except for a small bug. I have a table "nominated" which, besides some other data such as title-subitle etc, holds some tags separated by commas for each record in part. Example of record: title = toyota MR2 subtitle - damn fast car tags = fast, toyota, mr2, car The code that I use to put all the tags together and also build the cloud looks like this: mysql_select_db($database_anunturi); $query = "SELECT tags FROM nominated ORDER BY ID DESC"; $do_query = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_assoc($do_query); $totalRows = mysql_num_rows($do_query); if($totalRows>0) { $array = ''; do { $array .= $row['tags'] . ' '; } while($row = mysql_fetch_assoc($do_query)); } require('stopwords.php'); $stop = new Stopwords(); $array = $stop->parseString($array); $split = split(' ', $array); $words = ''; foreach($split as $key): $words .= $key . ','; endforeach; function count_list_to_array( $words ) { $list = array_count_values( split(", ?", $words ) ) ; return $list; } $list = count_list_to_array( $words ) ; arsort( $list ); foreach( $list as $index => $occured ) { printf("%s (%s)<br/>\n", $index, $occured ); } PHP: Everything works fine as I said except for 163 blank spaces that are counted. require('stopwords.php'); is for taking down viagras, punctuation and other characters (cleaning the content). The result is something like: (163) toyota(3) mr2(2) red(2) car(1) Any idea how to get rod of that white space?
$words = 'toyota, bmw, audi, opel, audi, bmw, renault,,,,,'; $tmp = preg_split('/,\s*/', $words, -1, PREG_SPLIT_NO_EMPTY); asort($tmp); foreach (array_count_values($tmp) AS $word => $count) { echo "{$word} ({$count})<br />\n"; } PHP:
mysql_select_db($database_anunturi); $query = "SELECT tags FROM nominated ORDER BY ID DESC"; $do_query = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_assoc($do_query); $totalRows = mysql_num_rows($do_query); if($totalRows>0) { $array = ''; do { $array .= $row['tags'] . ' '; } while($row = mysql_fetch_assoc($do_query)); } require('stopwords.php'); $stop = new Stopwords(); $array = $stop->parseString($array); $split = split(' ', $array); $words = ''; foreach($split as $key): $words .= $key . ','; endforeach; function count_list_to_array( $words ) { $list = array_count_values( array_values( split(", ?", $words ) ) ) ; return $list; } $list = count_list_to_array( $words ) ; arsort( $list ); foreach( $list as $index => $occured ) { if( $index ) { printf("%s (%s)<br/>\n", $index, $occured ); } } PHP:
Wow....digitapoint is the best. I ran out of net due to a storm here and never got the chance to thank you. I must admit that I asked on many forums and only here someone really helped. Thank you so much. It works. IT WORKS. IT's ALLLLIIIIVEEE!!! )
i am a fan of krakjoe.his codes are so interesting.also nico_swd,he is so infomative.i recently joined this forum and here i saw member's give solutions of problems.i have never seen such an excellent forums.i learnt many things here and i am still learning.