Pergunta

I would like to count the words in this array and have them display as a total rather than how many times each word is displayed.

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

Result

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

The result i would like

6

Foi útil?

Solução

You mean this ?

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

Alternatively you can use sizeof too !

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

Outras dicas

What you're looking for is count(). More information can be found here: http://uk3.php.net/count

Specifically:

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

You will need to use the count function.

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

This will print 6. You could also assign the count to a variable.

$count = count($array);

Use count function in php:

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

If you have multiple words in one array value, try this approach:

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

It implodes the array and gets number of words in the returned string.

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

If you want a function:

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

you should use count($array).

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

You can use count() function — it used count all elements in an array.

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

Ref: http://in3.php.net/count

$total_count = count(array_unique($array));
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top