以下为学习孔祥盛主编的《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); //
  17. echo "<br/>";
  18. print_r($countStudents1); //
  19. echo "<br/>";
  20. print_r($countStudents2); //
  21. ?>

输出:

  1. 4
  2. 16
  3. 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. ?>

输出:

  1. D:\wampServer\www\Apache服务器主目录\practise\例程.php:6:int 90
  2.  
  3. 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. ?>

输出:

  1. D:\wampServer\www\Apache服务器主目录\practise\例程.php:6:int 60
  2.  
  3. 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. ?>

输出:

  1. D:\wampServer\www\Apache服务器主目录\practise\例程.php:8:int 300
  2.  
  3. D:\wampServer\www\Apache服务器主目录\practise\例程.php:10:int 10
  4.  
  5. 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. ?>

输出:

  1. D:\wampServer\www\Apache服务器主目录\practise\例程.php:6:int 30240000
  2.  
  3. 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. ?>

输出:

  1. 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. ?>

输出:

  1. a
  2. b
  3. c
  4. d

5_PHP数组_3_数组处理函数及其应用_2_数组统计函数的更多相关文章

  1. 剑指Offer 13. 调整数组顺序使奇数位于偶数前面 (数组)

    题目描述 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变. 题目地址 https ...

  2. PHP如何判断一个数组是一维数组或者是二维数组?用什么函数?

    如题:如何判断一个数组是一维数组或者是二维数组?用什么函数? 判断数量即可 <?php if (count($array) == count($array, 1)) { echo '是一维数组' ...

  3. php extract 函数的妙用 数组键名为声明为变量,键值赋值为变量内容

    extract 函数的妙用 数组键名为声明为变量,键值赋值为变量内容 它的主要作用是将数组展开,键名作为变量名,元素值为变量值,可以说为数组的操作提供了另外一个方便的工具

  4. Atitit main函数的ast分析  数组参数调用的ast astview解析

    Atitit main函数的ast分析  数组参数调用的ast astview解析 1.1. Xxcls.main(new String[]{"","bb"}) ...

  5. 面试题-->写一个函数,返回一个数组中所有元素被第一个元素除的结果

    package com.rui.test; import java.util.Random; /** * @author poseidon * @version 1.0 * @date:2015年10 ...

  6. php函数、php定义数组和数组遍历

    <?php //php函数//1.简单函数//四要素:返回类型,函数名,参数列表,函数体 /*function Show(){ echo "hello";} Show();* ...

  7. C语言 数组做函数参数不传数组个数的遍历方法

    //数组做函数参数不传数组个数的遍历方法 #include<stdio.h> #include<stdlib.h> #include<string.h> void ...

  8. PHP基础语法: echo,var_dump, 常用函数:随机数:拆分字符串:explode()、rand()、日期时间:time()、字符串转化为时间戳:strtotime()可变参数的函数:PHP里数组长度表示方法:count($attr[指数组]);字符串长度:strlen($a)

    PHP语言原理:先把代码显示在源代码中,再通过浏览器解析在网页上 a. 1.substr;  //用于输出字符串中,需要的某一部分 <?PHP $a="learn php"; ...

  9. PHP数组的一些常用函数

    [数组排序]sort()低到高,rsort()高到低.保持键值对应关系使用 asort()和arsort().对键排序ksort()和krsort().随机排序 shuffle(). [数组key相关 ...

随机推荐

  1. 在Windows环境下搭建Snort+BASE入侵检测系统

    操作系统: Windows 7 (service pack 1) 所需软件: 虚拟机:VirtualBox 网络数据包截取驱动程序:WinPcap 4.1.3 (WinPcap_4_1_3.exe) ...

  2. ICEM-空心圆柱体

    原视频下载地址:https://pan.baidu.com/s/1boG49MB 密码: 4iq6

  3. Java封装jar包对外提供可执行文件

    编写Main方法,封装jar包 1.编写Main方法 import java.util.Date; /** * 描述 : * * @Author : zhanghao * @Time : 2019/1 ...

  4. .Net Core NOPI操作word(一)

    NOPI使用方式 1.安装nuget包 即可使用 Install-Package NPOI 一.创建word文档 //创建生成word文档 string path = "D:\\test.d ...

  5. 检测是否安装或者开启flash

    function flashChecker() { var hasFlash = 0; //是否安装了flash var flashVersion = 0; //flash版本 if(document ...

  6. odoo开发笔记 -- 异常处理in resolve_deps field = model

    场景描述: 更新代码,重启服务服务后,odoo后台报错,提示关键字:in resolve_deps field = model._fields[fname]  KeyError: 'entry_id' ...

  7. ASP.NET中httpmodules与httphandlers全解析

    https://www.cnblogs.com/zpc870921/archive/2012/03/12/2391424.html https://www.cnblogs.com/PiaoMiaoGo ...

  8. python爬虫中XPath和lxml解析库

    什么是XML XML 指可扩展标记语言(EXtensible Markup Language) XML 是一种标记语言,很类似 HTML XML 的设计宗旨是传输数据,而非显示数据 XML 的标签需要 ...

  9. openresty开发系列34--openresty执行流程之4访问阶段

    openresty开发系列34--openresty执行流程之4访问阶段 访问阶段 用途:访问权限限制 返回403 nginx:allow 允许,deny 禁止 allow ip:deny ip: 涉 ...

  10. [Kaggle] Online Notebooks

    前言 Let's go to https://www.kaggle.com/ Kaggle Notebook 有实践记录的案例. 一.线性拟合噪声数据 [Sklearn] Linear regress ...