5_PHP数组_3_数组处理函数及其应用_2_数组统计函数
以下为学习孔祥盛主编的《PHP编程基础与实例教程》(第二版)所做的笔记。
一、数组统计函数
数组统计函数是指统计数组各元素的值,并对这些值进行简单分析。

1. count() 函数
该函数的别名函数为 sizeof()
程序:
<?php
$colors = array("red"=>"red","green",3=>"white",5);
$students = array(
"2010001"=>
array("studentNo"=>"2010001","studentName"=>"张三","studentSex"=>"男"),
"2010002"=>
array("studentNo"=>"2010002","studentName"=>"李四","studentSex"=>"女"),
"2010003"=>
array("studentNo"=>"2010003","studentName"=>"王五","studentSex"=>"男"),
"2010004"=>
array("studentNo"=>"2010004","studentName"=>"马六","studentSex"=>"女")
);
$countColors = count($colors);
$countStudents1 = count($students,1); //mode参数值设为1
$countStudents2 = count($students); //mode参数值默认值为0
print_r($countColors); //
echo "<br/>";
print_r($countStudents1); //
echo "<br/>";
print_r($countStudents2); //
?>
输出:
4
16
4
2. max() 函数
程序:
<?php
$scores = array(70, 80, 90, 60);
$grades = array('A', 'B', 'C', 'D');
$maxScores = max($scores);
$maxGrades = max($grades);
var_dump($maxScores); //int 90
echo "<br/>";
var_dump($maxGrades); //string 'D' (length=1)
?>
输出:
D:\wampServer\www\Apache服务器主目录\practise\例程.php:6:int 90 D:\wampServer\www\Apache服务器主目录\practise\例程.php:8:string 'D' (length=1)
3. min() 函数
程序:
<?php
$scores = array(70, 80, 90, 60);
$grades = array('A', 'B', 'C', 'D');
$minScores = min($scores);
$minGrades = min($grades);
var_dump($minScores); //int 60
echo "<br/>";
var_dump($minGrades); //string 'A' (length=1)
?>
输出:
D:\wampServer\www\Apache服务器主目录\practise\例程.php:6:int 60 D:\wampServer\www\Apache服务器主目录\practise\例程.php:8:string 'A' (length=1)
4. array_sum() 函数
程序:
<?php
$scores = array(70, 80, 90, 60);
$grades1 = array('1A', '2B', '3C', '4D');
$grades2 = array('A1', 'B2', 'C3', '4D');
$sumScores = array_sum($scores);
$sumGrades1 = array_sum($grades1);
$sumGrades2 = array_sum($grades2);
var_dump($sumScores); //int 300
echo "<br/>";
var_dump($sumGrades1); //int 10
echo "<br/>";
var_dump($sumGrades2); //int 4 //字符串开头不是数字不会被转换为整数或浮点数
?>
输出:
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() 函数
程序:
<?php
$scores = array(70, 80, 90, 60);
$grades = array('1A', '2B', '3C', '4D');
$productScores = array_product($scores);
$productGrades = array_product($grades);
var_dump($productScores); //int 30240000
echo "<br/>";
var_dump($productGrades); //int 24
?>
输出:
D:\wampServer\www\Apache服务器主目录\practise\例程.php:6:int 30240000 D:\wampServer\www\Apache服务器主目录\practise\例程.php:8:int 24
6. array_count_values() 函数
程序:
<?php
$array = array(1, "hello", 1, "world", "hello");
print_r( array_count_values($array) );
?>
输出:
Array ( [1] => 2 [hello] => 2 [world] => 1 )
数组的遍历
使用数组统计 count() 函数 和 for 循环语句可以遍历连续整数 “键” 的数组。
程序:
<?php
$chars = range('a','d');
$counts = count($chars);
for($key=0; $key<$counts; $key++){
echo $chars[$key]."<br/>";
}
?>
输出:
a
b
c
d
5_PHP数组_3_数组处理函数及其应用_2_数组统计函数的更多相关文章
- 剑指Offer 13. 调整数组顺序使奇数位于偶数前面 (数组)
题目描述 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变. 题目地址 https ...
- PHP如何判断一个数组是一维数组或者是二维数组?用什么函数?
如题:如何判断一个数组是一维数组或者是二维数组?用什么函数? 判断数量即可 <?php if (count($array) == count($array, 1)) { echo '是一维数组' ...
- php extract 函数的妙用 数组键名为声明为变量,键值赋值为变量内容
extract 函数的妙用 数组键名为声明为变量,键值赋值为变量内容 它的主要作用是将数组展开,键名作为变量名,元素值为变量值,可以说为数组的操作提供了另外一个方便的工具
- Atitit main函数的ast分析 数组参数调用的ast astview解析
Atitit main函数的ast分析 数组参数调用的ast astview解析 1.1. Xxcls.main(new String[]{"","bb"}) ...
- 面试题-->写一个函数,返回一个数组中所有元素被第一个元素除的结果
package com.rui.test; import java.util.Random; /** * @author poseidon * @version 1.0 * @date:2015年10 ...
- php函数、php定义数组和数组遍历
<?php //php函数//1.简单函数//四要素:返回类型,函数名,参数列表,函数体 /*function Show(){ echo "hello";} Show();* ...
- C语言 数组做函数参数不传数组个数的遍历方法
//数组做函数参数不传数组个数的遍历方法 #include<stdio.h> #include<stdlib.h> #include<string.h> void ...
- PHP基础语法: echo,var_dump, 常用函数:随机数:拆分字符串:explode()、rand()、日期时间:time()、字符串转化为时间戳:strtotime()可变参数的函数:PHP里数组长度表示方法:count($attr[指数组]);字符串长度:strlen($a)
PHP语言原理:先把代码显示在源代码中,再通过浏览器解析在网页上 a. 1.substr; //用于输出字符串中,需要的某一部分 <?PHP $a="learn php"; ...
- PHP数组的一些常用函数
[数组排序]sort()低到高,rsort()高到低.保持键值对应关系使用 asort()和arsort().对键排序ksort()和krsort().随机排序 shuffle(). [数组key相关 ...
随机推荐
- SDN阅读作业
阅读文章<软件定义网络(SDN)研究进展>,并根据所阅读的文章,书写一篇博客,回答以下问题(至少3个): 1.为什么需要SDN?SDN特点? 随着网络规模的不断扩大,传统网络设备繁复的协议 ...
- 解决iis内存占用过大的问题--ZT
解决iis内存占用过大的问题 在IIS6下,经常出现w3wp的内存占用不能及时释放,从而导致服务器响应速度很慢. 今天研究了一下,可以做以下配置: 1.在IIS中对每个网站进行单独的应用程序池配置.即 ...
- Qt QSplitter任意拆分窗口
Qt拆分窗口可以使用QSplitter,也可以使用比较容易使用布局控件来完成,本文章给出使用QSplitter分割窗口的例子. QSplitter 继承自QFrame,而QFrame又继承自QWidg ...
- Docker搭建ES集群
Spring Boot连接ES,spring-boot-starter-data-elasticsearch. 必须为集群方式!否则报错! 报错: None of the configured nod ...
- redis的相关原理
一.AOF 二.RDB 三.哨兵
- clipboard 在 vue 项目中,on 事件监听回调多次执行
clipboard 定义一个全局变量 import ClipboardJS from "clipboard"; if(clipboard){ clipboard.destroy() ...
- springboot:redis反序列化发生类型转换错误
明明是同一个类,在反序列时报类型转换错误,真实奇怪.经查找资料,说是引入了devtools的缘故. 注释掉以下内容: <dependency> <groupId>org.spr ...
- LeetCode Top Interview Questions
LeetCode Top Interview Questions https://leetcode.com/problemset/top-interview-questions/ # No. Titl ...
- python:单例模式--使用__new__(cls)实现
单例模式:即一个类有且仅有一个实例. 那么通过python怎么实现一个类只能有一个实例呢. class Earth: """ 假如你是神,你可以创造地球 "&q ...
- springboot-把web项目打成war包部署到外部tomcat
将打包方式修改为war <packaging>war</packaging> 移除tomcat依赖或者将tomcat依赖scope改为provide 移除tomcat依赖 &l ...