Domanda

Ho una serie di tag che sto estraendo da un database, sto esportando i tag in una nuvola di tag. Sono bloccato per ottenere solo la prima istanza della parola. Ad esempio:

$string = "test,test,tag,tag2,tag3";

$getTags = explode("," , $string);
  foreach ($getTags as $tag ){
     echo($tag);
   }

Questo produrrebbe due volte il tag test. all'inizio pensavo di poter usare stristr per fare qualcosa del tipo:

  foreach ($getTags as $tag ){
      $tag= stristr($tag , $tag); 
        echo($tag);
   }

Questa è ovviamente una logica sciocca e non funziona, stristr sembra sostituire solo la prima occorrenza, quindi qualcosa come "test 123" eliminerebbe solo il "test" e restituirebbe "123" Ho visto che questo può essere fatto anche con regex ma non ne ho trovato un esempio dinamico.

Grazie,
Brooke

Modifica: unique_array () funziona se sto usando una stringa statica ma non funziona con i dati dal database perché sto usando un ciclo while per ottenere i dati di ogni riga.

    $getTag_data = mysql_query("SELECT tags FROM `news_data`");
if ($getTag_data)
{

   while ($rowTags = mysql_fetch_assoc($getTag_data))
   {
     $getTags = array_unique(explode("," , $rowTags['tags']));
        foreach ($getTags as $tag ){
        echo ($tag);
      }
   }
}
È stato utile?

Soluzione

Suppongo che ogni riga nella tua tabella contenga più di un tag, separati da coma, in questo modo:

Row0: php, regex, stackoverflow
Row1: php, variables, scope
Row2: c#, regex

In tal caso, prova questo:

$getTag_data = mysql_query("SELECT tags FROM `news_data`");

//fetch all the tags you found and place it into an array (with duplicated entries)
$getTags = array();
if ($getTag_data) {
   while ($row = mysql_fetch_assoc($getTag_data)) {
     array_merge($getTags, explode("," , $row['tags']);
   }
}

//clean up duplicity
$getTags = array_unique($getTags);

//display
foreach ($getTags as $tag ) {
   echo ($tag);
}

Sottolineo che questo non è efficiente.

Un'altra opzione (già menzionata qui) sarebbe quella di usare i tag come chiavi dell'array, con il vantaggio di poterli contare facilmente.
Potresti farlo in questo modo:

$getTag_data = mysql_query("SELECT tags FROM `news_data`");

$getTags = array();
if ($getTag_data) {
   while ($row = mysql_fetch_assoc($getTag_data)) {
     $tags = explode("," , $row['tags']);
     foreach($tags as $t) {
       $getTags[$t] = isset($getTags[$t]) ? $getTags[$t]+1 : 1;
     }
   }
}

//display
foreach ($getTags as $tag => $count) {
   echo "$tag ($count times)";
}
  • tieni presente che nessuno di questi codici è stato testato, solo per farti un'idea.

Altri suggerimenti

usa array_unique()

$string = "test,test,tag,tag2,tag3";

$getTags = array_unique(explode("," , $string));
foreach ($getTags as $tag ){
   echo($tag);
}

Usa le tue parole come chiavi del dizionario, non come valori.

$allWords=array()
foreach(explode("," , $string) as $word)
  $allWords[$word]=true;
//now you can extract these keys to a regular array if you want to
$allWords=array_keys($allWords);

Mentre ci sei, puoi anche contarli!

$wordCounters=array()
foreach(explode("," , $string) as $word)
{
  if (array_key_exists($word,$wordCounters))
     $wordCounters[$word]++;
  else
     $wordCounters=1;
}

//word list:
$wordList=array_keys($wordCounters);

//counter for some word:
echo $wordCounters['test'];

Credo che array_unique di php sia quello che stai cercando:

http://php.net/manual/en/function.array -unique.php

Utilizzare la funzione array_unique prima di iterare sull'array? Rimuove ogni stringa duplicata e restituisce le funzioni uniche.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top