php学习 数组课件第1/2页
下标:数组中的识别名称 也就是字符串或整数在数组中的代号
数组中有几个索引值就被称为几维数组。
索引值:索引是对数据库表中一列或多列的值进行排序的一种结构。
数组分类
在PHP数组被分为两种:
索引数组:索引(indexed)索引值是整数,以0开始,当通过位置来标识东西时用索引数组。
关联数组:关联(associative)关联以字符串做索引值,索引值为列名,用语访问列的数据。
数组通常用赋值的方式
一般情况下数组赋值有两种方式:
$a[1]="dsadsadsa";
$b[2]="dsadsadsad";
使用array函数:
$a=array("dsads","dsadsa",321312);
一维数组:数组的索引值(下标)只有一个的时候称之为一维数组。
数组直接赋值的格式:
$数组变量名[索引值]=资料内容;
注意:索引值可以是字符串也可以是整数 但是1与“1”是不同的 它们一个属于整数一个属于字符串。
同名没有给予索引值的数组是按照顺序排列的。
实例:
$a=array(1,2,3,4,5,6);
$b=array("one", "two", "three");
$c=array(0=>"aaa",1=>"bbb",2=>"ccc");
$d=array("aaa",6=>"bbb","ccc");
$e=array("name"=>"zhang", "age"=>20);
?>
二维数组
多维数组的格式:
$a[0][]="dsadas";
$a[0][]="dsadsa"; 这组是$a下的0索引值下的1和2
如果用array函数声明格式如下:
$a=array("dsadsa","dsadas",21,array("dsadsa","dsadas"));
数组的遍历
foreach循环结构:
foreach 仅用与数组的循环 两种格式
foreach(array_exprssion(数组表达) as $value);
foreach(array_exprssion(数组表达) as $key=>$value);
第一种格式遍历给定了array_exprssion数组。每一次循环中当前的值都被赋给我$calue,并且数组内部的指针向前移动一步。
第二种格式做同样的事,只除了当前单元的键值也会在每次循环中被赋给变量 $key。
当 foreach 开始执行时,数组内部的指针会自动指向第一个单元。此外注意foreach 所操作的是指定数组的一个拷贝,而不是该数组本身
$arr=array(10,20,30,40,50,60);
foreach($arr as $k=>$v){
echo "$k=>$v
";
}
输出结果:0=>10 1=>20 2=>30 3=>40 4=>50 5=>60//下标=>整数
联合使用list(),each()和while循环
each():
$arr=array(1,2,3,4,5);
$a=each($arr);
print_r($a);
输出结果:Array ( [1] => 1 [value] => 1 [0] => 0 [key] => 0 )
拿取数组第一位的值value 下标key
list():
$arr3=array("a","b","c");
list($key,$value)=each($arr3);
echo $key."
".$value;
输出结果:0 a list()可以说是用一步操作给一组变量赋值 仅能用于数字索引的数组并假定数字索引从 0 开始。
while循环
$arr=array(1,2,3,4,5,6,7,8,9,);
while(list($key,$value)=each($arr)){
$key++;
echo $key."=>".$value;
echo "
";
}
echo "
";
输出结果:1=>1 2=>2 3=>3 4=>4 5=>5 6=>6 7=>7 8=>8 9=>9
reset()数组指针重定向
在执行 each() 之后,数组指针将停留在数组中的下一个单元或者当碰到数组结尾时停留在最后一个单元。
is_array检测变量是否为数组真返回ture假false
$arr=array(1,2,3,4,5,6,"saas");
while(list($k,$v) = each($arr))
{
if(is_array($arr))
{
$x += $v;
echo $x;
}
else
{
$x += $k;
}
}
这个范例不能完全体现is_array的功能,但是可以参考。
数组的指针
next():负责将指针向后移动
prve():负责将指针向前移动
end():会将指针指向数组最后一个元素
reset():将目前指针无条件移至第一个索引位置
语法格式:mixed next(数组名称)
$arr=(array(1,2,3,4,5));
echo end($arr);
输出结果:5
key()与current()和count()
key()的函数是读取目前指针所指向的资料的索引值。
current()的函数则是读取目前指针所指向资料的内容资料。
count()的函数是用来计算数组中所有元素的个数,也就是说函数会传回目标数组的长度值。
格式:int count(数组名称);
key():从关联数组中取得键名
$array = array('fruit1' => 'apple','fruit2' => 'orange','fruit3' => 'grape','fruit4' => 'apple','fruit5' => 'apple');
while ($fruit_name = current($array)) {
if ($fruit_name == 'apple') {
echo key($array).'
';
}
next($array);
}
输出结果:fruit1,fruit4,fruit5
current():返回数组中的当前单元
$transport = array('foot', 'bike', 'car', 'plane');
$mode = current($transport); // $mode = 'foot';
$mode = next($transport); // $mode = 'bike';
$mode = current($transport); // $mode = 'bike';
$mode = prev($transport); // $mode = 'foot';
$mode = end($transport); // $mode = 'plane';
$mode = current($transport); // $mode = 'plane';
注意看范例 返回数组中的当前单元
count():计算数组中单元的个数
$arr=array(1,2,3,4,5,6);
echo count($arr);
输出结果:6
array_change_key_case()
array_change_key_case返回字符串键名全为小写或者大写的数组
其中包含的形态函数有两个[CASE_UPPER]转换为大写,[CAS_LOWER]转换为小写。
$input_array = array("FirSt" => 1, "SecOnd" => 4);
print_r(array_change_key_case($input_array, CASE_UPPER));
输出结果:Array ( [FIRST] => 1 [SECOND] => 4 )
array_chunk()
array_chunk()次函数会将目标数组的资料内容,以指定索引个数,分解成数个小型数组包含在原数组中。
$arr=array(1,2,3,4,5,6);
$a=array_chunk($arr,3);
print_r($a);
输出结果:Array ( [0] => Array ( [0] => 1 [1] => 2 [2] => 3 ) [1] => Array ( [0] => 4 [1] => 5 [2] => 6 ) )
也就是等于用数组单元个数的总和除以3
array_count_values
array_count_values 用来计算目标数组中各值出现的次数
语法格式:array_count_values(目标数组)
此函数所传回结果值,会以原数组的内容资料作为索引,以数组的形态表现。
$arr=array(1,2,3,3,2,6);
print_r(array_count_values($arr));
输出结果:Array ( [1] => 1 [2] => 2 [3] => 2 [6] => 1 )

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Validator can be created by adding the following two lines in the controller.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an
