Home  >  Article  >  Backend Development  >  How to count the number of array elements in php

How to count the number of array elements in php

怪我咯
怪我咯Original
2017-07-04 13:13:321899browse

This article mainly introduces relevant information about the method of counting the number of elements in phparray. Friends in need can refer to the following

count(): Yes 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 a regular The number of elements in the 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);

will create an array named $ac, which includes:

            关键字    值

             4     1

           5     1

           1     3

           2     2

           3     1

Posting a netizen’s idea is also very good

$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 detailed content of How to count the number of array elements in php. For more information, please follow other related articles on the PHP Chinese website!

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