1

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

Shankar Narayana Damodaran
  • 66,874
  • 43
  • 94
  • 124
Daisy Oopsy
  • 155
  • 9
  • 4
    you mean, like... count() ? – Justin T. Jan 07 '14 at 13:24
  • i think so, the count() can count even the arrays element. – Goikiu Jan 07 '14 at 13:24
  • Did you try to search an answer???Look http://stackoverflow.com/questions/1317612/count-number-of-values-in-array-with-a-given-value, http://stackoverflow.com/questions/4914175/php-count-array-elements – sergio Jan 07 '14 at 13:44

10 Answers10

5

You mean this ?

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

Alternatively you can use sizeof too !

echo sizeof($array); //"prints" 6
Shankar Narayana Damodaran
  • 66,874
  • 43
  • 94
  • 124
  • In fairness it wasn't specified if this is indeed the case or not. In which case, what happens with a single string of multiple words, would the OP expect those to be counted as 1 word, or N words within said string. – fantasitcalbeast Jan 07 '14 at 14:10
  • Granted. But that's a massive lack of information, given the very different approaches required. Don't you think? Had the OP stated: $array = ("something", "something else", "something else again"); - In their current question, maybe everyone would've got the point? You can't start flaming folk for offering an answer, when the question is (if it is at all), fundamentally flawed. – fantasitcalbeast Jan 07 '14 at 14:34
2

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
fantasitcalbeast
  • 5,105
  • 4
  • 36
  • 67
1

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);
The Humble Rat
  • 4,442
  • 6
  • 36
  • 71
1

Use count function in php:

echo count($array); // this will print lenght of the array
aksu
  • 5,163
  • 5
  • 22
  • 38
Pushker Yadav
  • 836
  • 1
  • 7
  • 14
1

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));
}
aksu
  • 5,163
  • 5
  • 22
  • 38
0

you should use count($array).

R R
  • 3,025
  • 2
  • 22
  • 42
0

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

Krish R
  • 22,188
  • 7
  • 49
  • 57
0
$total_count = count(array_unique($array));
aksu
  • 5,163
  • 5
  • 22
  • 38
Eric
  • 8,854
  • 12
  • 63
  • 99
0
$array = array("abstract", "accident", "achilles", "acidwash", "afrojack", "aguilera");
print_r(sizeof($array));
Awlad Liton
  • 9,260
  • 2
  • 26
  • 50
0
count($array);

or

sizeof($array);

http://bd1.php.net/manual/en/function.count.php

http://bd1.php.net/sizeof

chanchal118
  • 3,339
  • 2
  • 25
  • 50