Re: (Most frequently..) Corrected and added note

From: Date: Thu, 17 Jan 2008 23:18:51 +0000
Subject: Re: (Most frequently..) Corrected and added note
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-44530@lists.php.net to get a copy of this message
buzon@alejandro.ceballos.info wrote:
I notice the syntaxis error in my previous message array_slice(array_keys($hash_frequencywords,0,20)) must be array_slice(array_keys($hash_frequencywords),0,20); But the error continues generating me an "array enter" to my first array. The error occurs in the following piece of code: for ($i=0; $i<count($arr_frequencywords); $i++) { $str_thisword = $arr_frequencywords[$i]; echo "$i:$str_thisword "; $hash_frequencywords[$str_thisword]++; }
Break it down to make sure each step does what you want. // store in $arr_frequencyids all different words for($i=0; $i<count($arr_phrase); $arr_phrase) { array_push($arr_frequencyids,explode(" ",$arr_phrase[$i])); } Does that do what you want and give you an array in $arr_frequencyids ? <<< for ($i=0; $i<count($arr_frequencywords); $i++)
You're using a different variable name here. Where does this one come from? // Yeh i used the word 'long' twice // so it would appear at the top of the list. $string = "long piece of string that is really long"; $words = explode(' ', $string); // print_r($words); Once you get it to that stage, you can actually just use a php function to count everything. See http://www.php.net/manual/en/function.array-count-values.php print_r(array_count_values($words)); If you want the top 5: $counted_words = array_count_values($words); $flipped_array = array_flip($counted_words); asort($flipped_array); print_r(array_slice($flipped_array, 0, 5, true)); You could probably do it without flipping the array (see http://www.php.net/array_flip) but it makes sorting easier. -- Postgresql & php tutorials http://www.designmagick.com/

« previous php.db (#44530) next »