Home >Backend Development >PHP Tutorial >How to count the number of array elements in PHP_PHP Tutorial
This article mainly introduces the relevant information on the method of counting the number of array elements in php. Friends who need it can refer to it
Count(): Count the number of elements in the array;
Sizeof(): has the same purpose as count(). 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;
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);
An array named $ac will be created, which includes:
?
1
2
3
4
5
6
7
8
9
10
11 Keyword Value
4 1
5 1
1 3
2 2
3 1
It’s also very good to post a netizen’s idea
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14$arr = array(
'1011,1003,1008,1001,1000,1004,1012',
1009',
'1011,1003,1111'
);
$result = array();
foreach ($arr as $str) {
$str_arr = explode(',', $str);
foreach ($str_arr as $v) {
$result[$v] = isset($result[$v]) ? $result[$v] : 0;
$result[$v] = $result[$v] 1;
}
}
print_r($result);
The above is the entire content of this article, I hope you all like it.