计算数组中的单词总数

时间:2014-01-07 13:23:01

标签: php arrays function search counting

我想计算这个数组中的单词并将它们显示为总数而不是每个单词的显示次数。

<?php
$array = array("abstract", "accident", "achilles", "acidwash", "afrojack", "aguilera");
print_r(array_count_values($array));
?>

结果

Array ( [abstract] => 1 [accident] => 1 [achilles] => 1 [acidwash] => 1 [afrojack] => 1 [aguilera] => 1 )

我想要的结果

6

10 个答案:

答案 0 :(得分:5)

你是说这个?

echo count($array); //"prints" 6

或者您也可以使用sizeof

echo sizeof($array); //"prints" 6

答案 1 :(得分:2)

您正在寻找的是count()。可以在此处找到更多信息:http://uk3.php.net/count

具体做法是:

$b[0]  = 7;
$b[5]  = 9;
$b[10] = 11;
$result = count($b);
// $result == 3

答案 2 :(得分:1)

您需要使用计数功能。

$array = array("abstract", "accident", "achilles", "acidwash", "afrojack", "aguilera");
print_r(count($array));

这将打印6.您还可以将计数分配给变量。

$count = count($array);

答案 3 :(得分:1)

在php中使用count函数:

echo count($array); // this will print lenght of the array

答案 4 :(得分:1)

如果您在一个数组值中有多个单词,请尝试以下方法:

$wordcount = str_word_count(implode(' ', $array));

它会破坏数组并获取返回字符串中的单词数。

http://php.net/function.str-word-count.php
http://php.net/function.implode

如果你想要一个功能:

function array_word_count($array) {
    return str_word_count(implode(' ', $array));
}

答案 5 :(得分:0)

你应该使用count($array)

答案 6 :(得分:0)

您可以使用count()函数 - 它使用计算数组中的所有元素。

echo $count = count($array); //OP : 6 

参考:http://in3.php.net/count

答案 7 :(得分:0)

$total_count = count(array_unique($array));

答案 8 :(得分:0)

$array = array("abstract", "accident", "achilles", "acidwash", "afrojack", "aguilera");
print_r(sizeof($array));

答案 9 :(得分:0)