How to count the elements of an array in PHP

Source: Internet
Author: User
Tags foreach

This article mainly introduces the PHP statistics array elements of the method of the relevant data, the need for friends can refer to the

COUNT (): counts the number of elements in an array;

sizeof (): and Count () has the same purpose, both functions can return the number of elements in an array. You can get the number of elements in a regular scalar quantity, if the array passed to the function is an empty array, or a variable that is not set, the number of elements returned is 0;

Array_count_value (): Counts the number of times each particular value appears in the array $array;

Such as:

?

1

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

$ac =array_count_value ($array);

An array named $ac is created, which includes:

?

1

2

3

4

5

6

7

8

9

10

11 Keyword values

4 1

5 1

1 3

2 2

3 1

Send a Netizen's idea, also very good

?

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 mentioned is the entire content of this article, I hope you can enjoy.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.