July 14, 201412 yr I am trying to do the following and having some difficulty. I am trying to echo the words in the array as the key along with the total times the word appears in the string. Any advisewould be great. Part 1: Write a function countWords($str) that takes any string of characters and finds the number of times each word occurs. You should ignore the distinction between capital and lowercase letters, and do not have to worry about dealing with characters that are not letters. Hint: Create an associative array mapping word keys to the number of times they occur. You will need to look at PHP's string functions to split a sentence into words. Hint 2: The print_r($array_name) function is useful for examining the contents of an array. My code: <?php // my string $str = "My name is dale and i am a programmer learning php a programmer am i dale"; // my string to lower case $str = strtolower($str); // split string into words and put into an array $words = explode(' ', $str); // counts values of array $counts = array_count_values($words); foreach ($counts as $key => $value) { echo $value; } ?>
July 15, 201412 yr I am trying to do the following and having some difficulty. I am trying to echo the words in the array as the key along with the total times the word appears in the string. Any advisewould be great. My code: <?php // my string $str = "My name is dale and i am a programmer learning php a programmer am i dale"; // my string to lower case $str = strtolower($str); // split string into words and put into an array $words = explode(' ', $str); // counts values of array $counts = array_count_values($words); foreach ($counts as $key => $value) { echo $value; } ?> Now, explode turns your data into an array splitting the parts up by using the space as a splitter, so you don't need array counts to get the data back. <?php // my string $str = "My name is dale and i am a programmer learning php a programmer am i dale"; // my string to lower case $str = strtolower($str); // split string into words and put into an array $words = explode(' ', $str); foreach ($words AS $key => $value) { echo $value.'<br />'; } //Counts How many parts are in this array? echo 'There are '.count($words).' words in this array!'; ?> Edited July 15, 201412 yr by Penguin_Media
July 16, 201412 yr Author Brilliant so the count() function counts the total amount of words in a string. Is there a way to count how many of each words there are? For example the word 'programmer' count as twice. Is there a way to echo in a nicer format print_r($counts)? $str = "My name is dale and i am a programmer learning php a programmer am i dale"; $str = strtolower($str); $words = explode(' ', $str); $counts = array_count_values($words); print_r($counts); Edited July 16, 201412 yr by dpuk44
July 16, 201412 yr Brilliant so the count() function counts the total amount of words in a string. Is there a way to count how many of each words there are? For example the word 'programmer' count as twice. Is there a way to echo in a nicer format print_r($counts)? $str = "My name is dale and i am a programmer learning php a programmer am i dale"; $str = strtolower($str); $words = explode(' ', $str); $counts = array_count_values($words); print_r($counts); Add me up on skype: nitrophysix, Ill help you for free The count is counting how many parts there are in an array.
July 16, 201412 yr Is this what you are looking for <?php // my string $str = "My name is dale and i am a programmer learning php a programmer am i dale"; // my string to lower case $str = strtolower($str); // split string into words and put into an array $words = explode(' ', $str); $count = array(); // Loop throgh the resulys and start counting foreach ($words as $value) { // Check to see if the word exists if so increment the count if not start the count at 1 if (isset($count[$value])) { $count[$value]++; } else { $count[$value] = 1; } } // Display the results echo "<pre>".print_r($count, true)."</pre>"; ?> Edited July 16, 201412 yr by D4Y0
July 16, 201412 yr Output Array ( [my] => 1 [name] => 1 [is] => 1 [dale] => 2 [and] => 1 [i] => 2 [am] => 2 [a] => 2 [programmer] => 2 [learning] => 1 [php] => 1 ) Sorry for the double post the bb coded F'ed up the code formatting
July 16, 201412 yr you could use this solution <?php // my string $str = strtolower("My name is dale and i am a programmer learning php a programmer am i dale"); // split string into words and put into an array $words = explode(' ', $str); $vals = array_count_values($words); echo "<pre>"; print_r($vals); echo "</pre>"; foreach($vals as $k => $v) { echo "<b>".$k. "</b> occurs " .$v." times in this sentence <br/>"; } ?> it is self explanatory i hope this script will return: Array([my] => 1[name] => 1[is] => 1[dale] => 2[and] => 1 => 2[am] => 2[a] => 2[programmer] => 2[learning] => 1 => 1)my occurs 1 times in this sentence name occurs 1 times in this sentence is occurs 1 times in this sentence dale occurs 2 times in this sentence and occurs 1 times in this sentence i occurs 2 times in this sentence am occurs 2 times in this sentence a occurs 2 times in this sentence programmer occurs 2 times in this sentence learning occurs 1 times in this sentence php occurs 1 times in this sentence Edited July 16, 201412 yr by inlinedivblock
July 16, 201412 yr Author thats fantastic everyone, really appreciate it. i will study the code and put it to practice in another scenario to learn from. Thanks again
Create an account or sign in to comment