【问题标题】:php only get the first occurrence of a word from an arrayphp 只从数组中获取第一次出现的单词
【发布时间】:2009-11-25 22:10:05
【问题描述】:

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

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

【问题讨论】:

    标签: php regex dynamic-data strstr


    【解决方案1】:

    使用array_unique()

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

    【讨论】:

    • 这看起来应该可以通过阅读 php 手册来工作。如果我使用的是静态字符串,但如果我从 MySQL 数据库中提取数据,它就可以工作。我认为这可能是因为我从多行中拉出并爆炸每一行。
    【解决方案2】:

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

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

    【讨论】:

    • 这是赢家,IMO。也应该是最快的。
    【解决方案3】:

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

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

    【讨论】:

    • 感谢成功,我不得不在 foreach 循环中添加$t = trim($t);,将键设置为“test”与“test”不同。感谢您的帮助!
    【解决方案4】:

    我相信 php 的 array_unique 就是你要找的:

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

    【讨论】:

      【解决方案5】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-01-23
        • 2022-12-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多