oz11 Posted January 8 Share Posted January 8 This is my code.. though it only sorts by occurrences atm.. how do i get it to sort by alphabetical order?? <?php function most_frequent_words($string, $stop_words = [], $limit = 10000) { $string = strtolower($string); // Make string lowercase $words = str_word_count($string, 1); // Returns an array containing all the words found inside the string $words = array_diff($words, $stop_words); // Remove black-list words from the array $words = array_count_values($words); // Count the number of occurrence arsort($words); // Sort based on count return array_slice($words, 0, $limit); // Limit the number of words and returns the word array } $join_terms = ""; $stmt = $pdo->prepare("select terms from links"); $stmt->execute([]); while ($row = $stmt->fetch()) { //echo $row['terms']."<br />\n"; $join_terms = " ".$join_terms . $row['terms']." "; } $dupl = implode(' ', array_unique(explode(' ', $join_terms))); $dupl = str_replace(",", "", $dupl); $stop_words = array(""); $frequent = most_frequent_words($dupl, $stop_words); //print_r($frequent); ?> <small id="pop_terms"> <?php if(is_array($frequent)){ foreach($frequent as $k=>$value) { $encode = base64_encode($k); echo "<a href='../tags.php?q=". $encode."'>". $k."</a>, " ; // $k is the key if ($k === array_key_last($frequent)) { echo "."; } } } ?> I must have nabbed abit of code from somewhere a while ago . Just cant get my head around it. Helpz plz. Quote Link to comment Share on other sites More sharing options...
Barand Posted January 8 Share Posted January 8 ksort($words) ? 1 Quote Link to comment Share on other sites More sharing options...
oz11 Posted January 8 Author Share Posted January 8 Hey. I shortened the code.. <?php $join_terms = ""; $stmt = $pdo->prepare("select terms from links"); $stmt->execute([]); while ($row = $stmt->fetch()) { //echo $row['terms']."<br />\n"; $join_terms = " ".$join_terms . $row['terms']." "; } $frequent = implode(' ', array_unique(explode(' ', $join_terms))); $frequent = str_replace(",", "", $frequent); print_r($frequent); ?> Can I loop through the $frequent array and then finally sort? Quote Link to comment Share on other sites More sharing options...
gizmola Posted January 8 Share Posted January 8 To understand the code, you should debug what the $words array looks like. The reason the code currently works to sort by occurrence, is because the array looks like this: array ( 'red' => 7, 'mailbox' => 2, ) Barand gave you the answer, but you need to understand what the difference is between the two functions and when to use one vs the other. 1 Quote Link to comment Share on other sites More sharing options...
oz11 Posted January 8 Author Share Posted January 8 Turns out its not an array Quote Link to comment Share on other sites More sharing options...
oz11 Posted January 8 Author Share Posted January 8 (edited) Hey. Thanks for the help.. here is my working code 4 anyone else..,. <?php $join_terms = ""; $stmt = $pdo->prepare("select terms from links"); $stmt->execute([]); while ($row = $stmt->fetch()) { $join_terms = " ".$join_terms . $row['terms'].", "; } $frequent = implode(' ', array_unique(explode(' ', $join_terms))); $myArray = explode(',', $frequent); sort($myArray); print_r($myArray); Edited January 8 by oz11 Quote Link to comment Share on other sites More sharing options...
Barand Posted January 8 Share Posted January 8 Assuming your data looks something like this table... ### CREATE SOME TEST DATA FIRST $pdo->exec("CREATE TEMPORARY TABLE links (terms varchar(20))"); $pdo->exec("INSERT INTO links (terms) VALUES ('rhubarb'), ('dog'), ('ferret'), ('zucchini'), ('daffodil'), ('yeti'), ('misery'), ('ferret')"); then $stmt = $pdo->query("SELECT DISTINCT terms FROM links ORDER BY terms "); $results = array_column($stmt->fetchAll(), 'terms'); echo '<pre>' . print_r($results, 1) . '</pre>'; giving Array ( [0] => daffodil [1] => dog [2] => ferret [3] => misery [4] => rhubarb [5] => yeti [6] => zucchini ) Quote Link to comment 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.