Jump to content

Sorting using sort()..? But how with this..?


oz11

Recommended Posts

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.  

  • Thanks 1
Link to comment
Share on other sites

Posted (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 by oz11
Link to comment
Share on other sites

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
)

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.