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
  • 68,075
  • 43
  • 96
  • 126
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
  • 68,075
  • 43
  • 96
  • 126
  • 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. – laminatefish 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. – laminatefish 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
laminatefish
  • 5,197
  • 5
  • 38
  • 70
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,586
  • 6
  • 39
  • 73
1

Use count function in php:

echo count($array); // this will print lenght of the array
aksu
  • 5,221
  • 5
  • 24
  • 39
Pushker Yadav
  • 866
  • 1
  • 8
  • 15
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,221
  • 5
  • 24
  • 39
0

you should use count($array).

R R
  • 2,999
  • 2
  • 24
  • 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,583
  • 7
  • 50
  • 59
0
$total_count = count(array_unique($array));
aksu
  • 5,221
  • 5
  • 24
  • 39
Eric
  • 9,870
  • 14
  • 66
  • 102
0
$array = array("abstract", "accident", "achilles", "acidwash", "afrojack", "aguilera");
print_r(sizeof($array));
Awlad Liton
  • 9,366
  • 2
  • 27
  • 53
0
count($array);

or

sizeof($array);

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

http://bd1.php.net/sizeof

chanchal118
  • 3,551
  • 2
  • 26
  • 52