PHP Questions Home
Categories
Arrays
Files
Forms
Creating PDFs
PHP Errors
Functions
Images
MySQL
Numbers
Others
Strings
Website

PHP Forums
Forums Index
MySQL Forum
PHP Forum
Flash/Actionscript
Javascript
Web design

More PHP
PHP Functions
Top Questions
Ask a Question
PHP Interview Questions
PHP Jobs
Ecommerce
IT Questions
Business Websites

Sponsors

sponsor ad
My Salary
PHP Developer Salary
Advertise Here

How do I count the number of times a value appears in an array?


This is easy with one of PHPs built-in functions. No need to write your own function to take the strain, simply use array_count_values.

Here's an example of this easy to use function so that you can see just how it works:

<?php

$values 
= array(1,1,1,2,2,2,2,3,3,4,2,5,1,3,5,3,1,5);
$most_popular array_count_values($values);
print_r($most_popular);

?>


This prints the following:

(
[1] => 5
[2] => 5
[3] => 4
[4] => 1
[5] => 3
)

So you can see it returns an array with the values of the initial array as the keys of the new array, with the number of times each value was present in the initial array as the value in the new array.



ASK A QUESTION

More arrays PHP Questions

How do you remove duplicate values from an array?

what is a array?

how a values entered in an array from keyboard?

How do I change: function wordlimit($string, $length = 50, $ellipsis = "...") { $words = explode(' ', strip_tags($string)); if (count($words) > $length) return implode(' ', array_slice($words, 0, $length)) . $ellipsis; else

How do I pick a random value from an array?



Custom Search