dongxian5735 2015-03-11 18:11
浏览 51
已采纳

如何从php数组返回重复值,以便为SEO创建短语?

I have a text content that is placed inside $content_array. The result must be one array containing words that are repeated, separated with commas. Now, how do I take out all the words that are repeating in that text, and put them in another array. My code so far (won't work):

$content = strip_tags($content);
// removing all html and php tags from content

$content = strtolower($content);
// transform all characters to lowercase

$content_array = str_word_count($content, 1);   
// converting content to array, in order to easier search the content, every member of array is one word 

$duplicates = array_unique(array_diff_assoc($content_array, array_unique($content_array))); 
// checking for all the words that are repeating and storing them into $duplicates array

    while ($val = current($duplicates)){            
    // going through an array to check for repeated values

        if($val >= '3'){                                
        // searching for repeated words, the ones that show up X times and more than X times (X = 3)

            $phrase = array($val);                  
            // storing value of repeated word into $phrase array

        }
        next($duplicates);                          
        // moving on to the next member in array

    }
$phrase = implode(",", $phrase);                
// separate phrases with comma
  • 写回答

1条回答 默认 最新

  • duangao8359 2015-03-11 18:46
    关注

    I think the problem is that you overwrite $phrase each time so you need to use $phrase[] = $val; to append to the array, but this is simpler:

    $content = strip_tags($content);
    $content = strtolower($content);
    $content_array = str_word_count($content, 1);   
    $phrase = array_filter(array_count_values($content_array), function($v) { 
                                                                   return ($v >= 3);
                                                               });
    

    Or instead of the array_filter:

    foreach($content_array as $key => $value) {
        if($value >= 3) {
            $phrase[] = $key;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入