编程语言
首页 > 编程语言> > PHP数组_5_3_数组处理函数及其应用_2_数组统计函数

PHP数组_5_3_数组处理函数及其应用_2_数组统计函数

作者:互联网

以下为学习孔祥盛主编的《PHP编程基础与实例教程》(第二版)所做的笔记。

 

一、数组统计函数

数组统计函数是指统计数组各元素的值,并对这些值进行简单分析。

1. count() 函数

该函数的别名函数为 sizeof()

程序:

 1 <?php
 2 $colors = array("red"=>"red","green",3=>"white",5);
 3 $students = array(
 4     "2010001"=>
 5         array("studentNo"=>"2010001","studentName"=>"张三","studentSex"=>"男"),
 6     "2010002"=>
 7         array("studentNo"=>"2010002","studentName"=>"李四","studentSex"=>"女"),
 8     "2010003"=>
 9         array("studentNo"=>"2010003","studentName"=>"王五","studentSex"=>"男"),
10     "2010004"=>
11         array("studentNo"=>"2010004","studentName"=>"马六","studentSex"=>"女")
12 );
13 $countColors = count($colors);
14 $countStudents1 = count($students,1);    //mode参数值设为1
15 $countStudents2 = count($students);    //mode参数值默认值为0
16 print_r($countColors);  //4
17 echo "<br/>";
18 print_r($countStudents1);    //16
19 echo "<br/>";
20 print_r($countStudents2);    //4
21 ?>

输出:

4
16
4

 

2. max() 函数

程序:

1 <?php
2 $scores = array(70, 80, 90, 60);
3 $grades = array('A', 'B', 'C', 'D');
4 $maxScores = max($scores);
5 $maxGrades = max($grades);
6 var_dump($maxScores);   //int 90
7 echo "<br/>";
8 var_dump($maxGrades);   //string 'D' (length=1)
9 ?>

输出:

D:\wampServer\www\Apache服务器主目录\practise\例程.php:6:int 90

D:\wampServer\www\Apache服务器主目录\practise\例程.php:8:string 'D' (length=1)

 

3. min() 函数

程序:

1 <?php
2 $scores = array(70, 80, 90, 60);
3 $grades = array('A', 'B', 'C', 'D');
4 $minScores = min($scores);
5 $minGrades = min($grades);
6 var_dump($minScores);   //int 60
7 echo "<br/>";
8 var_dump($minGrades);   //string 'A' (length=1)
9 ?>

输出:

D:\wampServer\www\Apache服务器主目录\practise\例程.php:6:int 60

D:\wampServer\www\Apache服务器主目录\practise\例程.php:8:string 'A' (length=1)

 

4. array_sum() 函数

程序:

 1 <?php
 2 $scores = array(70, 80, 90, 60);
 3 $grades1 = array('1A', '2B', '3C', '4D');
 4 $grades2 = array('A1', 'B2', 'C3', '4D');
 5 $sumScores = array_sum($scores);
 6 $sumGrades1 = array_sum($grades1);
 7 $sumGrades2 = array_sum($grades2);
 8 var_dump($sumScores);   //int 300
 9 echo "<br/>";
10 var_dump($sumGrades1);   //int 10
11 echo "<br/>";
12 var_dump($sumGrades2);   //int 4    //字符串开头不是数字不会被转换为整数或浮点数
13 ?>

输出:

D:\wampServer\www\Apache服务器主目录\practise\例程.php:8:int 300

D:\wampServer\www\Apache服务器主目录\practise\例程.php:10:int 10

D:\wampServer\www\Apache服务器主目录\practise\例程.php:12:int 4

 

5. array_product() 函数

 程序:

1 <?php
2 $scores = array(70, 80, 90, 60);
3 $grades = array('1A', '2B', '3C', '4D');
4 $productScores = array_product($scores);
5 $productGrades = array_product($grades);
6 var_dump($productScores);   //int 30240000
7 echo "<br/>";
8 var_dump($productGrades);   //int 24
9 ?>

 输出:

D:\wampServer\www\Apache服务器主目录\practise\例程.php:6:int 30240000

D:\wampServer\www\Apache服务器主目录\practise\例程.php:8:int 24

 

6. array_count_values() 函数

程序:

1 <?php
2 $array = array(1, "hello", 1, "world", "hello");
3 print_r( array_count_values($array) );
4 ?>

输出:

Array ( [1] => 2 [hello] => 2 [world] => 1 )

 

数组的遍历

使用数组统计 count() 函数 和 for 循环语句可以遍历连续整数 “键” 的数组。

程序:

1 <?php
2 $chars = range('a','d');
3 $counts = count($chars);
4 for($key=0; $key<$counts; $key++){
5     echo $chars[$key]."<br/>";
6 }
7 ?>

输出:

a
b
c
d

 

 

 

标签:www,practise,int,主目录,例程,数组,Apache,PHP,处理函数
来源: https://www.cnblogs.com/xiaoxuStudy/p/11823909.html