计算一个单词在数组中显示的次数?

时间:2013-08-24 02:19:04

标签: php

我正在使用array_push添加到数组中,其中有多少次卡片存在。

$array = array();
foreach($aiarray as $card) {
    $cardss = card($card);
    array_push($array, $cardss);
}

asort($array);
print_r($array);

这给了我我需要的东西,但现在我如何搜索数组并说出来;

if (*array contains > 2 of the same word) {
    find out what word;
    does the array contain more than one?;
}

2 个答案:

答案 0 :(得分:1)

这个怎么样?

if (in_array($word, $array)) { 
  $array_keys = array_flip($array);
  $number_of_words = count(array_keys($array_keys, $word));
  if ($number_of_words > 1) {
    echo "array contains word more than once";
  }
}

使用的功能:in_arrayarray_fliparray_keys

答案 1 :(得分:1)

Array Count Values然后PHP Get Highest Value from Array

只做

$count = array_count_values($array);
$max_times = max($count);
if ($max_times > 2) {
    $word = array_search($max_times, $count);
    .... whatever you need
}