Home >Backend Development >PHP Tutorial >How to get the length of an array or the number of occurrences of a certain value in PHP_PHP Tutorial

How to get the length of an array or the number of occurrences of a certain value in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:07:00890browse

How to get the length of an array or the number of occurrences of a certain value in PHP

This article mainly introduces the method of getting the length of an array or the number of occurrences of a certain value in PHP. The example analyzes count () and array_count_value(), friends in need can refer to it

The example in this article describes how PHP obtains the length of an array or the number of occurrences of a certain value. Share it with everyone for your reference. The specific analysis is as follows:

count(): Count the number of elements in the array;

For example:

?

1

2

$arr = Array('0','1','2','3','4');

echo count($arr); // 输出 5

1 2

$arr = Array('0','1','2','3','4');


echo count($arr); // Output 5

sizeof() and count() have the same purpose. Both functions can return the number of array elements. You can get the number of elements in a regular scalar variable. If the array passed to this function is an empty array , or a variable that has not been set, the number of array elements returned is 0;

1

2

$array=array(4,5,1,2,3,1,2,1);

$ac=array_count_value($array);

array_count_value() counts the number of times each specific value appears in the array $array; For example:


?


1

2

$array=array(4,5,1,2,3,1,2,1); $ac=array_count_value($array);

will create an array named $ac, which includes: Keyword Value 4 1 5 1 1 3 2 2 3 1 I hope this article will be helpful to everyone’s PHP programming design.
http://www.bkjia.com/PHPjc/956989.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/956989.htmlTechArticleHow to get the length of an array or the number of occurrences of a certain value in PHP. This article mainly introduces how to get the length of an array or the number of occurrences of a certain value in PHP. Method for the number of occurrences of a value, an example analysis of count() and array_count_value(...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn