php - Excluding duplicate words from array and Randomise and limit array -


im creating tag cloud article subject titles. each title, split words , put them in array, check word strlen > 3 , not in excluded words array. works great....

the bits im struggling are:

  • how randomise order , limit output 20
  • excluding duplicates, , duplicates mean duplicate words in same catid.

for example below word dog repeated 5 times in 3 different catid's. therefore output word dog 3 times once each distinct catid. array:

'subject' => 'dog running', 'id' => '1', 'catid' => '19'  'subject' => 'dog walking', 'id' => '2', 'catid' => '18'  'subject' => 'dog sitting', 'id' => '3', 'catid' => '18'  'subject' => 'dog eating', 'id' => '4', 'catid' => '19'  'subject' => 'dog barking', 'id' => '5', 'catid' => '20' 

here code:

$excluded_word_array = array('a','blah','bleh');  // prepare tag cloud array display $terms = array(); // create empty array  $query = mysql_query("select * hesk_kb_articles type = '0'"); while($row = mysql_fetch_array($query)){          $subject = $row['subject'];         $id = $row['id'];         $catid = $row['catid'];         $words = explode(" ", $subject);         foreach ($words $val){                 if (strlen($val) > 3) {                         $stripped_val = strtolower(ereg_replace("[^a-za-z]", "", $val));                         if (!in_array($stripped_val, $excluded_word_array)) {                         shuffle($stripped_val);                         $terms[] = array('subject' => $stripped_val, 'id' => $id, 'catid' => $catid);                         }                 }         } }  sort($terms); ?> 

you can use group by that:

$query = mysql_query("select * hesk_kb_articles type = '0' group subject, catid"); 

also mysql* functions deprecated of php 5.5.0, , removed in future. instead, mysqli or pdo_mysql extension should used

update1:

maybe can :

$excluded_word_array = array('a','blah','bleh'); $query = mysql_query("select * hesk_kb_articles type = '0'"); while($row = mysql_fetch_array($query)){      $subject = $row['subject'];     $id = $row['id'];     $catid = $row['catid'];     $words = explode(" ", $subject);     foreach ($words $val){         if (strlen($val) > 3) {             $stripped_val = strtolower(preg_replace("[^a-za-z]", "", $val));             if (!in_array($stripped_val, $excluded_word_array)) {                 $terms[$catid][] = $stripped_val;             }         }     } }  $items = array(); foreach ($terms $term) {     $term = array_unique($term);     $items = array_merge($items, $term); } 

$items contain words want .

update 2:

if want catid along words, changes last loop:

$i = 0; $items = array(); foreach ($terms $term_key => $term_value) {     $term_value = array_unique($term_value);     $items[$i]['catid'] = $term_key;     $items[$i]['words'] = implode(',', $term_value);     $i++; } 

now $items contain catid , words separated comma.

update 3:

if want each catid , words seperate can :

$i = 0; $items = array(); foreach ($terms $term_key => $term_value) {     $term_value = array_unique($term_value);     foreach ($term_value $term) {          $items[$i]['catid'] = $term_key;          $items[$i]['words'] = $term;          $i++;     } } 

hope helps :)


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -