All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

array_count_values In Php

Last Updated : Mar 11, 2024

array_count_values In Php

In this tutorial we will show you the solution of array_count_values in PHP, here, below today we see what is array_count_values() and for what purpose it is used in php.

In php, array_count_values () is used to counts all the values of an array.

Inside this function mainly only parameter is used and that is the name of array who’s which we want to count the values.

Step By Step Guide On array_count_values In PHP :-

Let us see the general syntax of array_count_values().

array_count_values(array)

Above is general syntax of array_count_values(). In below example, we will show you how to use this function in an array.

Let us see the codes for better understanding.

<!DOCTYPE html>
<html>
<head>
<title>
Example of array_count_values() in php
</title>
</head>
<body>
 <?php
$array=array("I","am","Human","I","Human");
print_r(array_count_values($array));
?></body>
</html>
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As above now <head> tag is used to contain information about web page. In this tag a <title> tag is used which helps us to specify a webpage title. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  4. Here, then we create a body tag. All the content which we want to show on browser’s screen or display is always written inside this codes.
  5. As, we see in the above codes. Inside body, we create a basic structure of php and <?php is the opening tag of php whereas ?> is closing tag of php.
  6. Inside these tags, we create a variable named array that is $array and starts with $ sign. After this we assigns an array to this variable $array using array () function.
  7. As you see that inside this array we give 5 values that are I am Human I am. Also, we know that the array always starts with 0 index. So, the index of last element is 4.
  8. After, this we use array_count_values () function and as we know inside this we have to give one parameter and that is the name of array whose values we want to count. So, we assigns $array to this function and to see the output that in what form the result is provided by this function, we use print_r() there.
  9. We can also use directly print_r() there and we can store the values from array_count_values() inside a variable and then use print_r() on that.
  10. This function gives us result in which I is followed by 2, human is followed by 2 and am is followed by 1. It means that I is used 2 times in array. similarly, human is used 2 times, but am is used 1 time.
  11. We hope you easily understand array_count_values() which helps use to count the number of times values used in an array.
  12. At last, the <body> and <html> tags are closed with </body> and </html> respectively.

Conclusion :-

At last in conclusion, here we can say that with the help of this article we are able to understand about array_count_values() in php.

I hope this tutorial on array_count_values in PHP helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪