oz11 Posted January 8, 2024 Share Posted January 8, 2024 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 https://forums.phpfreaks.com/topic/317608-sorting-using-sort-but-how-with-this/ Share on other sites More sharing options...
Barand Posted January 8, 2024 Share Posted January 8, 2024 ksort($words) ? 1 Quote Link to comment https://forums.phpfreaks.com/topic/317608-sorting-using-sort-but-how-with-this/#findComment-1614002 Share on other sites More sharing options...
oz11 Posted January 8, 2024 Author Share Posted January 8, 2024 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 https://forums.phpfreaks.com/topic/317608-sorting-using-sort-but-how-with-this/#findComment-1614003 Share on other sites More sharing options...
gizmola Posted January 8, 2024 Share Posted January 8, 2024 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 https://forums.phpfreaks.com/topic/317608-sorting-using-sort-but-how-with-this/#findComment-1614004 Share on other sites More sharing options...
oz11 Posted January 8, 2024 Author Share Posted January 8, 2024 Turns out its not an array Quote Link to comment https://forums.phpfreaks.com/topic/317608-sorting-using-sort-but-how-with-this/#findComment-1614005 Share on other sites More sharing options...
oz11 Posted January 8, 2024 Author Share Posted January 8, 2024 (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, 2024 by oz11 Quote Link to comment https://forums.phpfreaks.com/topic/317608-sorting-using-sort-but-how-with-this/#findComment-1614006 Share on other sites More sharing options...
Barand Posted January 8, 2024 Share Posted January 8, 2024 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 https://forums.phpfreaks.com/topic/317608-sorting-using-sort-but-how-with-this/#findComment-1614007 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.