-4

My array is

$myarray=array("Human","Angel",God"","Angel","Devil","God","God","Human","God","Angel");

How to get the word and count like

God : 4
Angel : 3
Human : 2
Devil : 1
2
  • Have you tried anything? Commented Jan 30, 2013 at 17:32
  • 4
    This is a "read the documentation" question. Commented Jan 30, 2013 at 17:32

3 Answers 3

3

Take a look at array_count_values and arsort.

<?php  
$myarray = array("Human","Angel","God","Angel","Devil","God","God","Human","God","Angel");

$result = array_count_values($myarray);
arsort($result);

foreach($result as $word => $count)
{
    echo $word." was found ".$count." time(s)<br/>";
}
?>
Sign up to request clarification or add additional context in comments.

Comments

3

See array_count_values.

From the documentation:

Example #1 array_count_values() example
<?php
  $array = array(1, "hello", 1, "world", "hello");
  print_r(array_count_values($array));
?> 

Array
(
  [1] => 2
  [hello] => 2
  [world] => 1
)

Comments

1

You can use the array-count-values function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.