Home > Backend Development > PHP Tutorial > How to count the number of array elements in php, count the number of arrays in php_PHP tutorial

How to count the number of array elements in php, count the number of arrays in php_PHP tutorial

WBOY
Release: 2016-07-13 09:47:25
Original
1140 people have browsed it

php method to count the number of array elements, php counts the number of arrays

count(): counts 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(): Count the number of times each specific value appears in the array $array;

For example:

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

Copy after login

will create an array named $ac, which includes:

            关键字    值

             4     1

           5     1

           1     3

           2     2

           3     1

Copy after login

It’s also very good to post a netizen’s idea

$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);
Copy after login

The above is the entire content of this article, I hope you all like it.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1026056.htmlTechArticlephp method to count the number of array elements, php counts the number of arrays count(): count the number of elements in the array Number statistics; sizeof(): has the same purpose as count(), both functions can return numbers...
Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template