php只获取数组中第一次出现的单词

发布于 2024-08-12 15:49:51 字数 943 浏览 12 评论 0原文

我有一组从数据库中提取的标签,我正在将标签导出到标签云中。我坚持只获取该词的第一个实例。例如:

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

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

这将输出测试标签两次。起初我以为我可以使用 stristr 来做类似的事情:

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

这显然是愚蠢的逻辑并且不起作用,stristr 似乎只替换第一次出现,所以像“ test 123”只会摆脱“test”并返回“123”我已经看到这也可以使用正则表达式来完成,但我还没有找到动态的例子。

谢谢,
布鲁克

编辑: unique_array() 如果我使用静态字符串,则可以工作,但无法处理数据库中的数据,因为我正在使用 while 循环来获取每行数据。

    $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);
      }
   }
}

I have an array of tags that I'm pulling from a database, I am exporting the tags out into a tag cloud. I'm stuck on getting only the first instance of the word. For example:

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

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

This would output the test tag twice. at first i thought i could use stristr to do something like:

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

This is obviously silly logic and doesn't work, stristr seems to only replace the first occurrence so something like "test 123" would only get rid of the "test" and would return "123" I've seen this can also be done with regex but I haven't found a dynamic exmaple of that.

Thanks,
Brooke

Edit: unique_array() works if I'm using a static string but won't work with the data from the database because I'm using a while loop to get each rows data.

    $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);
      }
   }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

指尖微凉心微凉 2024-08-19 15:49:51

使用array_unique()

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

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

use array_unique()

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

$getTags = array_unique(explode("," , $string));
foreach ($getTags as $tag ){
   echo($tag);
}
焚却相思 2024-08-19 15:49:51

将您的单词用作字典的键,而不是值。

$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);

当你在做的时候,你也可以数一下它们!

$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'];

Use your words as keys to the dictionary, not as values.

$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);

While you are at it, you can also count them!

$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'];
猫烠⑼条掵仅有一顆心 2024-08-19 15:49:51

我假设表中的每一行都包含多个标记,以逗号分隔,如下所示:

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

如果是这种情况,请尝试以下操作:

$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);
}

我会指出这效率不高。

另一种选择(已经在这里提到)是使用标签作为数组键,其优点是能够轻松地对它们进行计数。
你可以这样做:

$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)";
}
  • 请记住,这些代码都没有经过测试,这只是为了让你明白这个想法。

I'm assuming that each row in your table contains more than one tag, separated by coma, like this:

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

If that's the case, try this:

$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);
}

I'd point out that this is not efficient.

Another option (already mentioned here) would be to use the tags as array keys, with the advantage of being able to count them easily.
You could do it like this:

$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)";
}
  • please keep in mind none of this code was tested, it's just so you get the idea.
早茶月光 2024-08-19 15:49:51

我相信 php 的 array_unique 就是您正在寻找的:

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

I believe php's array_unique is what you are looking for:

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

一腔孤↑勇 2024-08-19 15:49:51

在迭代数组之前使用 array_unique 函数?它删除每个重复的字符串并返回唯一的函数。

Use the array_unique function before iterating over the array? It removes every duplicate string and return the unique functions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文